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.

Because some jobs are more important than others, theHacker General has invented and implemented a simple priority system for the print job queue. Now, each job is assigned a priority between 1 and 9 (with 9 being the highest

priority, and 1 being the lowest), and the printer operates

as follows.

The first job J in queue is taken from the queue.

If there is some job in the queue with a higher priority than job J, then move J to the end of thequeue without printing it.

Otherwise, print job J (and do not put it back in the queue).

In this way, all those important muffin recipes that the Hacker General is printing get printed very quickly. Of course, those annoying term papers that others are printing may have to wait for quite some time to get printed, but that’s life.

Your problem with the new policy is that it has become quite tricky to determine when your print job will actually be completed. You decide to write a program to figure this out. The program will be given the current queue (as a list of priorities) as well as the position of your job in the queue, and must then calculate how long it will take until your job is printed, assuming that no additional jobs will be added to the queue. To simplify matters, we assume that printing a job always takes exactly one minute, and that adding and removing jobs from the queue is instantaneous.

Input

One line with a positive integer: the number of test cases (at most 100). Then for each test case:

One line with two integers n and m, where n is the number of jobs in the queue (1 ≤ n ≤ 100)and m is the position of your job (0 ≤ m ≤ n − 1). The first position in the queue is number 0,the second is number 1, and so on.

One line with n integers in the range 1 to 9, giving the priorities of the jobs in the queue. The first integer gives the priority of the first job, the second integer the priority of the second job, and so on.

Output

For each test case, print one line with a single integer; the number of minutes until your job is completely printed, assuming that no additional print jobs will arrive.

Sample Input

3

1 0

5

4 2

1 2 3 4

6 0

1 1 9 1 1 1

Sample Output

1

2

5

题解:优先队列问题,模拟完成即可。

注意:

priority_queue<int> v 对v队列的根据优先级从大到小排序

push(x) 将x压入队列的末端

pop() 弹出队列的第一个元素(队顶元素),注意此函数并不返回任何值

front() 返回第一个元素(队顶元素)

back() 返回最后被压入的元素(队尾元素)

empty() 当队列为空时,返回true

size() 返回队列的长度

top() 返回优先队列中有最高优先级的元素

#include <iostream>

#include <cstdio>

#include <queue>

using namespace std;

int main()

{

int t;

scanf("%d", &t);

while (t--)

{

queue<int> q;

priority_queue<int> v;

int n, m;

scanf("%d%d", &n, &m);

int i;

for (i = 0; i < n; ++i)

{

int a;

scanf("%d", &a);

q.push(a);

v.push(a);

}

while (true)

{

int x = q.front();

q.pop();

if (m == 0)  //如果m==0,则证明现在打印的是目标任务

{

if (x != v.top())  //如果队列中还有优先级比x高的..

{

m = v.size() - 1;//下标是从0开始的

q.push(x);//将该任务放到队尾

}

else

{

break;

}

}

else    //如果现在的任务还不是目标任务

{

--m;

if (x != v.top())

{

q.push(x);

}

else

{

v.pop();

}

}

}

printf("%d\n", n - q.size());//q.size是全部打印结束后q队列的长度

}

return 0;

}

时间: 2024-07-29 22:06:46

12100 Printer Queue(优先队列)的相关文章

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

题目链接: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

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

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

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: 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

用一个队列模拟,还有一个数组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

hdu 1972.Printer Queue 解题报告

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