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

题目描述:

题目思路:

使用一个队列记录数字,一个优先队列记录优先级,如果相等即可打印;

 1 #include <iostream>
 2 #include <queue> 
 3 using namespace std;
 4 int main(int argc, char *argv[])
 5 {
 6     int t;
 7     cin >> t; 
 8     while(t--)
 9     {
10         int n,pos;
11         queue<int> q ;
12         priority_queue<int> pq ;
13         cin >> n >> pos ;
14         for(int i=0;i<n;i++)
15         {
16             int a;
17             cin >> a;
18             q.push(a);
19             pq.push(a);
20         }
21         int t = 0;
22         while(true)
23         {
24             if(q.front() == pq.top())
25             {
26                 if(t==pos){cout<<n-q.size()+1<<endl;break;}
27                 else{
28                     q.pop();
29                     pq.pop();
30                     t++;
31                 }
32             }
33             else{
34                 int temp = q.front();
35                 q.pop();
36                 q.push(temp);
37                 if(t == pos){t=0;pos=q.size()-1;} 
38                 else t++ ;
39                 
40             }
41         }
42     }
43     return 0;
44 }

猜你喜欢

转载自www.cnblogs.com/secoding/p/9495600.html