1. 程式人生 > >Printer Queue

Printer Queue

#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<stdio.h>
using namespace std;

class node{
public:
	int x;
	bool flag;
	node(int i,bool f):x(i),flag(f){
	}
};

node* a[110];
int n,m;

void pai(){           //這個函式是模擬題目的操作,即每次對隊頭的一個元素進行判斷,若他的後面有priority比他高的元素,那麼他就掉到隊尾,否則不移動。
	int i,j;      //最後得到先後順序都固定的陣列。由於下標為m的元素,用flag==true標記了,所以只要找到他,得到他的下標,就知道要花費的時間。
	for(i=0;i<n-1;){  //這裡操作的是陣列類似於實現佇列的思想。不過沒有靈活利用資料結構。這裡實際上可以用queue代替,更容易理解和程式設計。
		for(j=i+1;j<n;j++){
			if(a[i]->x<a[j]->x){      //這裡將元素調到陣列末尾要實現插入資料,用陣列效率低,還好輸入規模不大。這裡可以用連結串列替代來實現。
				node* temp=a[i];
				int k=i+1;
				for(;k<n;k++){        
					a[k-1]=a[k];
				}
				a[n-1]=temp;
				break;
			}else{
				if(j==n-1)
					i++;
			}
		}
	}
}

int main(){
	int t;
	cin>>t;
	while(t--){
		int i;
		cin>>n>>m;
		int x;
		for(i=0;i<n;i++){
			cin>>x;
			if(i==m){
				a[i]=new node(x,true);
			}else{
				a[i]=new node(x,false);
			}
		}
		pai();
		for(i=0;i<n;i++){
			if(a[i]->flag){
				cout<<(i+1)<<endl;
				break;
			}
		}
	}
	return 0;
}

相關推薦

打印隊列 (Printer Queue,ACM/ICPC NWERC 2006,UVA12100)

priority clu names 技術 使用 info 一個 esp 描述 題目描述: 題目思路: 使用一個隊列記錄數字,一個優先隊列記錄優先級,如果相等即可打印; 1 #include <iostream> 2 #include <

Printer Queue列印佇列

The only printer in the computer science students' union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs in the printer qu

列印佇列(Printer Queue)

Printer Queue Time limit: 3.000 seconds 【分析】        首先記錄所求時間它在佇列中的位置,用一個佇列儲存這些任務的優先順序,同時也建立一個佇列儲存對應

Printer Queue

#include<iostream> #include<algorithm> #include<stack> #include<queue> #incl

UVa12100 Printer Queue (列印佇列)

12100  Printer Queue Input    One line with a positive integer :  the number of test cases(at most 100).  Then for each test case:    

2647. Printer Queue

The only printer in the computer science students' union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs in the printer qu

poj 3125 Printer Queue (佇列)

The only printer in the computer science students' union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs in the printer que

Printer Queue(poj 3125)

The only printer in the computer science students' union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs in the printer qu

5-7 UVA 12100 Printer Queue列印佇列

大致題意: 輸入一定數量的列印任務,並伴隨有優先順序,按優先順序從大到小的順序列印,從隊首(位置為0)開始列印,發現隊後存在一個列印任務的優先順序高於自己,則自己放於隊後,先列印優先順序高的。 整體思路: 不用想,根據題意,存資料(優先順序)用肯定用queue存(當然有很多

關於“列印佇列 Printer Queue, ACM/ICPC NWERC 2006, UVa 12100)"的討論

列印佇列有兩個屬性,一個是優先順序一個是位置,可以寫一個結構體儲存這些資訊再利用queue的特性完成本題 #include<iostream> #include<queue> #include<vector> using namespac

POJ 3125 Printer Queue(單鏈表)

The only printer in the computer science students' union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs in the printer qu

Printer Queue(列印佇列)POJ3125

The only printer in the computer science students' union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs in the printer qu

uva 12100 Printer Queue(列印佇列)模擬

思路:按照輸入的順序,將每個任務入隊,同時用另一個數組儲存這些優先順序,然後按照從大到小或者從小到大的順序對陣列排序,表示每個執行的先後順序。然後對佇列進行操作,先從佇列頭取出一個元素,判斷是不是和陣

Codeforce 545D. Queue

pac not num title log upper swap lap pin Little girl Susie went shopping with her mom and she wondered how to improve service quality.

python 多線程並發threading & 任務隊列Queue

不同 htm doc threading 阻塞 子線程 per 出現 bag https://docs.python.org/3.7/library/concurrency.htmlpython程序默認是單線程的,也就是說在前一句語句執行完之前後面的語句不能繼續執行先感受一

C++ 隊列queue中的一段玄學代碼

原因 gin p s emp printf empty ont ffffff pro 代碼: mtx.lock(); printf("node %d push localW prepared,and local flag=%d,clock=%d\n",procID,l

進擊的Python【第九章】:paramiko模塊、線程與進程、各種線程鎖、queue隊列、生產者消費者模型

password locking form maxsize 廁所 sorted [0 hostname nbsp 一、paramiko模塊 他是什麽東西?   paramiko模塊是用python語言寫的一個模塊,遵循SSH2協議,支持以加密和認證的方式,進行遠程服務器的連

[leetcode-406-Queue Reconstruction by Height]

pytho pos line and sin solution ont input concise Suppose you have a random list of people standing in a queue. Each person is described

Queue

num note lin highlight from define ber mes div Description There are n walruses standing in a queue in an airport. They are numbered st

C++ STL學習 queue

pop out 第一個 code 兩個 push stl queue 註意 本文修改自http://www.cnblogs.com/hdk1993/p/5809180.html 1、使用queue需要聲明頭文件#include <queue> 2、queu