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;
11     Task(int pos = 0, int pri = 0):pos(pos), pri(pri) {}
12 };
13
14 int cnt[10];
15
16 int main()
17 {
18     //freopen("in.txt", "r", stdin);
19
20     int T; scanf("%d", &T);
21     while(T--)
22     {
23         int n, p;
24         scanf("%d%d", &n, &p);
25         memset(cnt, 0, sizeof(cnt));
26         queue<Task> Q;
27         for(int i = 0; i < n; i++)
28         {
29             int pri;
30             scanf("%d", &pri);
31             Q.push(Task(i, pri));
32             cnt[pri]++;
33         }
34         while(1)
35         {
36             Task t = Q.front();
37             int m;
38             for(m = 9; m >= t.pri; m--) if(cnt[m]) break;
39             cnt[m]--;
40             if(m > t.pri)
41             {
42                 while(t.pri != m)
43                 {
44                     Q.pop();
45                     Q.push(t);
46                     t = Q.front();
47                 }
48                 Q.pop();
49                 if(t.pos == p) break;
50                 //printf("sz = %d\n", Q.size());
51             }
52             else
53             {
54                 Q.pop();
55                 if(t.pos == p) break;
56             }
57         }
58         printf("%d\n", n - Q.size());
59     }
60
61     return 0;
62 }

代码君

时间: 2024-11-08 19:14:06

UVa 12100 (模拟) 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

UVa 133 The Dole Queue

 The Dole Queue  In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be placed in a large circle, facing inwards. Someone i

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

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

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

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 (习题 5-7)

传送门:https://uva.onlinejudge.org/external/121/12100.pdf 题意:队列中待打印的任务(1 <= n <= 100)带有优先级(1-9), 打印步骤为每次从队首拿出一个, 如果队列中没有优先级比该任务高的, 打印这个任务; 若有优先级高的, 把这个任务放到队尾,  并打印优先级最高的. 每打印一次耗时1分钟, 求给定任务什么时候打印. 水题A半天    不愧是弱渣.......... 最坏的情况需要maxn*maxn的空间........ fro

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)