SOJ 1443. Printer Queue

题目大意:输入整数t,代表测试样例的数量。每个测试样例首先输入整数n和整数m,分别代表队列中作业的数目和目标作业当前位置。然后下一行输入n个大小为1-9的整数,分别代表n个作业的优先级。每次检查队列头部,若队列头部作业的优先级是队列中的最大值,则抛出作业;否则将该作业放入队尾重新排队。要求输出目标作业是第几次被抛出的。

解题思路:构建结构体,包含作业的id(用作业开始的位置作为id)和作业的优先级。模拟抛出作业的过程。利用优先队列保存队列的优先级,能够更高效的判断队列优先级的最大值。

代码如下:

 1 #include <iostream>
 2 #include <queue>
 3 using namespace std;
 4
 5 struct Elem {
 6     int pos;
 7     int priority;
 8     Elem(int pos_ = 0, int priority_ = 1) {
 9         pos = pos_;
10         priority = priority_;
11     }
12 };
13
14 int main() {
15     int t;
16     cin >> t;
17     while (t--) {
18         int n, m;
19         int temp;
20         queue<Elem> elems;
21         priority_queue<int> pq;
22         cin >> n >> m;
23         for (int i = 0; i < n; i++) {
24             cin >> temp;
25             elems.push(Elem(i, temp));
26             pq.push(temp);
27         }
28
29         int step = 0;
30         while (true) {
31             step++;
32             int next_priority = pq.top();
33             pq.pop();
34             // cout << next_priority << endl;
35
36             while (elems.front().priority != next_priority) {
37                 elems.push(elems.front());
38                 elems.pop();
39             }
40
41             if (elems.front().pos == m) {
42                 break;
43             } else {
44                 elems.pop();
45             }
46         }
47
48         cout << step << endl;
49
50     }
51
52     return 0;
53 }
时间: 2024-09-28 21:00:37

SOJ 1443. Printer Queue的相关文章

12100 Printer Queue(优先队列)

12100 Printer Queue12 The only printer in the computer science students’ union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs in the printer queue and you may have to wait for hours to get a single page of output. Bec

Printer Queue

Description The only printer in the computer science students' union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs in the printer queue and you may have to wait for hours to get a single page of output. Because some

poj 3125 Printer Queue (队列)

 Printer Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3679   Accepted: 1975 Description The only printer in the computer science students' union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs i

POJ 3125 Printer Queue 数据结构 队列

Printer Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4329   Accepted: 2269 Description The only printer in the computer science students' union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs in t

UVa12100,Printer Queue

水题,1A过的 数据才100,o(n^3)都能过,感觉用优先队列来做挺麻烦的,直接暴力就可以了,模拟的队列,没用stl #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <queue> #define maxn 100+5 using namespace std; int mid[maxn],v[maxn],q[maxn*maxn

hdu 1972.Printer Queue 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1972 题目意思:需要模拟打印机打印.打印机里面有一些 job,每个job被赋予1-9的其中一个值,越大表示优先级越高,越早被打印.job这个队列是会向前推进的,如果排在最前面的job优先级最高,那么才打印,否则就把这个job放到队列最后.问给出 m 这个位置的job要经过多长时间才被打印. 规定每次打印时间为一分钟,移动 job到队列最后不占时间. 练开优先队列就继续吧---不过这题不是咯. 仅仅用

UVa: 12100 - Printer Queue

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3252 题目描述:有一些文件需要打印机打印,每个人物有不同的优先级(1-9),打印机的运作方式为:首先从打印队列里取出一个任务J,如果队列里有比J更急的任务,则直接把任务放到打印队列的尾部,否则打印任务J.输入打印队列中各个任务的优先级以及所关注的任务在队列中的位置(对首位置为0).

UVA 12100 Printer Queue(队列和优先队列,水)

1 //#include<bits/stdc++.h> 2 #include<cstdio> 3 #include<queue> 4 #include<algorithm> 5 using namespace std; 6 typedef long long ll; 7 /** 8 题意:所有任务在队列中,若当前打印的任务优先级不是最大,则移动到队列尾部.问下标为k的任务在什么时刻被打印: 9 思路:用优先队列判断优先级是否最高.用队列模拟任务 10 (1)

UVa 12100 (模拟) Printer Queue

用一个队列模拟,还有一个数组cnt记录9个优先级的任务的数量,每次找到当前最大优先级的任务然后出队,并及时更新cnt数组. 1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <cstdio> 5 #include <queue> 6 using namespace std; 7 8 struct Task 9 { 10 int pos, pri