UVA 12100 Printer Queue

题意:求关注位置的元素被打印后,所花费的总时间。

思路:

  用一个普通队列 p 和优先队列 pq 存优先级,因为优先队列队首肯定是要被打印的元素;

  如果p,pq队首相等,表示可以打印,不然就丢到后面去。

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<vector>
 5 using namespace std;
 6 
 7 
 8 int n;
 9 int main()
10 {
11     cin >> n;
12     while (n--)
13     {
14         int m,pos;
15         cin >> m>>pos;
16         queue<int> q;
17         priority_queue<int>pq;
18         for (int i = 0; i < m; i++)
19         {
20             int level;
21             cin >> level;
22             q.push(level);
23             pq.push(level);
24         }
25 
26         int cnt = 0,index=0;//花费的时间和当前的下标
27         while (true)
28         {
29             if (q.front() == pq.top())//当前优先级最高,可以打印
30             {
31                 if (index == pos)//得到结果
32                 {
33                     cnt++;
34                     cout << cnt << endl;
35                     break;
36                 }
37                 else
38                 {
39                     cnt++;
40                     q.pop();
41                     pq.pop();
42                     index++;
43                 }
44             }
45             else//不等把当前扔到最后面
46             {
47                 int cur = q.front();
48                 q.push(cur);
49                 q.pop();
50                 if (index == pos)
51                 {
52                     pos = q.size() - 1;
53                     index = 0;
54                 }
55                 else
56                 {
57                     index++;
58                 }
59             }
60         }
61     }
62     return 0;
63 }

 

posted @ 2019-02-15 15:39  付玬熙  阅读(270)  评论(0编辑  收藏  举报