UVa 12100打印队列(队列)

原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3252

这题可以建立两个队列,一个为优先级队列,当两者队首相同时就出队列,否则将队首放到队尾。

 1 #include<iostream>
 2 #include<queue>
 3 using namespace std;
 4 
 5 queue<int> p;
 6 priority_queue<int>pq;
 7 
 8 int main()
 9 {
10     int t, n, m, i, j, k;
11     cin >> t;
12     while (t--)
13     {
14         cin >> n >> m;
15         k = n;
16         while (!p.empty()) p.pop();
17         while (!pq.empty()) pq.pop();
18         while (n--)
19         {
20             cin >> i;
21             p.push(i);
22             pq.push(i);
23         }
24         int count = 0;
25         for (int i = 0; i <= m; i++)
26         {
27             if (p.front() != pq.top())
28             {
29                 j = p.front();
30                 p.push(j);
31                 p.pop();
32                 if (i == m)   m = m + k;            //如果第m个关注的任务不能打印,则在循环k次
33             }
34             else
35             {
36                 p.pop();
37                 pq.pop();
38                 count++;
39                 k--;
40             }
41         }
42         cout << count << endl;
43     }
44     return 0;
45 }

 

转载于:https://www.cnblogs.com/zyb993963526/p/6098230.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值