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:
• One line with two integers n and m, where n is the number of jobs in the queue (1≤n≤100)
And m is the position of your job (0≤m≤n−1). The first position in the queue is number 0,
the second is number 1,and soon.
• One line with n integers in the range 1 to 9, giving the priorities of the jobs in the queue. The
first integer gives the priority of the first job, the second integer the priority of the second job,
and soon.
Output
For each test case,print one line with a single integer;the number of minutes until your job is completely
printed,as suming that no additional print jobs will arrive.
Sample Input
3
1 0
5
4 2
1 2 3 4
6 0
1 1 9 1 1 1
Sample Output
1
2
5
My Solution
#include <iostream> #include <deque> using namespace std; bool compar(const deque<int> &de) { int cot=0; for(int i=1;i<de.size();i++) //最後一次比較是比較到最後一個的前一個和最後一個 /*! ^^^^^^^^^^^ 輪到的這個和後面所有的分別比較*/ { if(de[0]>=de[i]) cot++; //從第二個開始跟它比 } if(cot==de.size()-1) return true; //這裡完美的把單個元素的集合包含進去了 else return false; } int main() { deque<int> priqu; int T=0,n,place,tem,time; cin>>T; while(T--){ cin>>n>>place; priqu.clear(); for(int i=0;i<n;i++){ cin>>tem; priqu.emplace_back(tem); } time=0; int gol=place; while(gol>-1){ //對於本來就在首位i的就不對了,故不是gol>0而是>-1 int i=priqu[0]; //priqu.pop_front();必須放裡面 gol--; //******因為下面這裡要用到 int siz0=priqu.size(); if(compar(priqu)) { time++; //列印才要時間哦 priqu.pop_front(); continue; } else { priqu.pop_front(); priqu.emplace_back(i); if(gol==-1) gol+=siz0; //!應當是==-1的時候才加,所以加個size()就好了,剛好等於size()-1 } } cout<<time<<endl; } return 0; }
謝謝