POJ 3125 Printer Queue (priority queue)
来源:互联网 发布:怎么买氰化钾 淘宝 编辑:程序博客网 时间:2024/04/29 22:59
Printer Queue
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 3736 Accepted: 2015
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 jobs are more important than others, the Hacker 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.
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 simplifymatters, we assume that printing a job always takes exactly one minute, and that adding and removing jobs from the queue is instantaneous.
Because some jobs are more important than others, the Hacker 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, thenmove J to the end of the queue without printing it.
- Otherwise, print job J (and do not put it back in the queue).
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 simplifymatters, 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 linewith 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
31 054 21 2 3 46 01 1 9 1 1 1
Sample Output
125
Source
Northwestern Europe 2006
题目链接 :http://poj.org/problem?id=3125
题目大意 :有n个打印任务,你的打印任务位置为m,每个工作被附上1-9的优先级,每次操作队列首端的任务,若其优先级不为最高,则将其移至队列最后,否则打印该任务,设移动是瞬时完成的,现在要求你的打印任务什么时候完成
题目分析 :由于打印顺序是由优先级由高到低顺序进行的,因此本题为优先对列,因为操作涉及将第一个元素放到队尾,再考虑结合循环队列,可以将你的打印任务标记为负数,循环操作的终止条件为队列为空或者找到负数,每打印一次cnt++, 最后输出cnt的值即可
代码 :
#include <cstdio>int const MAX = 100 + 2;int a[MAX];int abs(int a){ return a > 0 ? a : -a;}int main(){ int ca; scanf("%d", &ca); while(ca--) { int n, m; scanf("%d %d", &n, &m); int st = 0, ed = n; //队首及队尾指针 for(int i = 0; i < n; i++) scanf("%d", &a[i]); a[m] = -a[m]; //将目标标记为负 int cnt = 0; while(ed + 1 != st) { int tmp = a[st]; //取出队首元素 st++; //移动队首指针 bool print = true; for(int i = st; i != ed; i++) { if(abs(tmp) < abs(a[i])) { print = false; //若不为优先级最高的不打印 a[ed] = tmp; //将其放到队尾 ed ++; //移动队尾指针 break; } } if(print) { cnt ++; //打印一次cnt++ if(tmp < 0) { printf("%d\n", cnt); //找到你的打印任务输出cnt break; } } } }}
0 0
- POJ 3125 Printer Queue (priority queue)
- poj 3125 Printer Queue
- POJ 3125 Printer Queue
- Printer Queue(poj 3125)
- poj 3125 Printer Queue
- POJ 3125 Printer Queue
- POJ 3125 Printer Queue 笔记
- poj 3125.Printer Queue
- POJ 3125 Printer Queue 模拟队列
- poj 3125 Printer Queue (队列)
- POJ 3125 Printer Queue(单链表)
- POJ 3125 Printer Queue 数据结构 队列
- priority queue
- Printer Queue
- Printer Queue
- Printer Queue
- Printer Queue
- queue队列-printer queue
- Python大数据处理工具
- 闲话行尸第四季陨
- 十大豪华SUV吞
- 汪峰抢头条5连败坏
- 晃动设备、读取本地的Html文件、避免cell子视图的重用,分享到Facebook Twitter
- POJ 3125 Printer Queue (priority queue)
- Android应用如何支持屏幕多尺寸多分辨率问题
- 法规法规发的噶实打实大师
- dss湿哒哒实打实的
- Oracle修改ip或主机名后重建em和监听
- 的方式的方式的发生地方
- 士大夫士大夫士大夫士大夫
- 学习敏捷开发(trello)之party_bid卡片3,4总结
- 显示进度条