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 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).
In this way, all those importantmuffin 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 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 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
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 题目思路:通过队列模拟过程。一个队列q保存输入的数,一个优先队列v。如果q队列的第一个数是v队列中优先级最大的数,就弹出它。如果不是,就把它放在队列后面。一直这样,直到弹出需要的数 代码如下: (借鉴了,他人博客,╮(╯▽╰)╭... C++小白,求不追究责任....)
#include<iostream>
#include<queue>
using namespace std;
int main()
{
int t, n, m, x;
cin>>t;
while(t--)
{
cin>>n>>m;
queue<int>q;
priority_queue<int>v;//优先队列,一声明,就排列,按照优先级从大到小
for(int i=0; i<n; i++)
{
cin>>x;
q.push(x);//将输入的X放到q队列的队尾
v.push(x);//将输入的X放到V队列的队尾
}
while(1)
{
x=q.front();//将q队列的第一个数赋值给x,用于接下来的判断
q.pop();//弹出q队列的第一个元素,先弹出来,接下来再判断
if(m==0)
{
if(x!=v.top())//弹出来的数不是最大优先级
{
m=v.size()-1;//目标的位置变化
q.push(x);//将x(q.front)放到q队列的队尾
}
else// 是最大优先级
break;
}
else
{
m--;
if(x!=v.top()) //
q.push(x);
else
v.pop();//弹出v队列的第一个元素,也就是最大优先级的数
}
}
cout<<n-q.size()<<endl;//n减去q队列剩下的数目就是打印目标需要的时间
}
return 0;
}
/* push(x) 将x压入队列的末端
pop() 弹出队列的第一个元素(队顶元素),注意此函数并不返回任何值
front() 返回第一个元素(队顶元素)
back() 返回最后被压入的元素(队尾元素)
empty() 当队列为空时,返回true
size() 返回队列的长度
top() 返回优先队列中有最高优先级的元素*/
Printer Queue的更多相关文章
- POJ 3125 Printer Queue
题目: Description The only printer in the computer science students' union is experiencing an extremel ...
- 12100 Printer Queue(优先队列)
12100 Printer Queue12 The only printer in the computer science students’ union is experiencing an ex ...
- uva 12100 Printer Queue
The only printer in the computer science students' union is experiencing an extremely heavy workload ...
- [刷题]算法竞赛入门经典(第2版) 5-7/UVa12100 - Printer Queue
题意:一堆文件但只有一个打印机,按优先级与排队顺序进行打印.也就是在一个可以插队的的队列里,问你何时可以打印到.至于这个插队啊,题目说"Of course, those annoying t ...
- J - Printer Queue 优先队列与队列
来源poj3125 The only printer in the computer science students' union is experiencing an extremely heav ...
- UVa 12100 Printer Queue(queue或者vector模拟队列)
The only printer in the computer science students' union is experiencing an extremely heavy workload ...
- Printer Queue UVA - 12100
The only printer in the computer science students' union is experiencing an extremely heavy workload ...
- hdu 1972.Printer Queue 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1972 题目意思:需要模拟打印机打印.打印机里面有一些 job,每个job被赋予1-9的其中一个值,越大 ...
- UVa 12100 (模拟) Printer Queue
用一个队列模拟,还有一个数组cnt记录9个优先级的任务的数量,每次找到当前最大优先级的任务然后出队,并及时更新cnt数组. #include <iostream> #include < ...
随机推荐
- cmd运行java,含传参,引用jar
1,创建一个java project,完成编码 在Eclipse的资源管理器中选中你要打包的项目,右键点击,选择“导出”项,弹出导出对话框,在下面的Java目录下选择“JAR 文件”项,下一步,在导出 ...
- 移动互联网(APP)产品设计的经验分享【转】
随着移动互联网的发展,越来越多的Web产品开始布局移动端,因此最近经常碰到PM们在交流讨论移动APP产品的设计.我从事移动互联网已经有一年多了,通过不断的学习和实践也积累了一些心得,今天整理并分享一下 ...
- C中的回调函数
C语言中应用回调函数的地方非常多,如Nginx中: struct ngx_command_s { ngx_str_t name; ngx_uint_t type; char *(*set)(ngx_c ...
- [Oracle EBS APIs]import Flow routing and DJ routing using BOM_RTG_PUB.PROCESS_RTG APIs
DJ routing --BOM模块的工艺路线 Flow routing -- Flow Manufacturing 模块使用的工艺路线,导入 Flow routing时先导入Line Operati ...
- 【推理,贪心】UVa 1319 - Maximum
看到了大神的代码.理解了好久...真是差距. 题意:给出m, p, a, b,然后xi满足已下两个公式, 求 xp1 + xp2 +...+ xpm 的最大值. 1.-1/sqrt(a) <= ...
- Dubbo服务调用的动态代理和负载均衡
Dubbo服务调用的动态代理及负载均衡源码解析请参见:http://manzhizhen.iteye.com/blog/2314514
- hdu 4614 线段树
思路:当k为1的时候,用二分法查询包含有f个空瓶的上界r,然后更新会方便很多,直接更新区间(a,r)了. #include<iostream> #include<cstdio> ...
- Java Concurrency - wait & notify, 等待通知机制
生产者消费者问题是一个常见的多线程同步案例:一组生产者线程和一组消费者线程共享一个初始状态为空.大小为 N 的缓冲区.只有当缓冲区没满的时候,生产者才能把消息放入缓冲区,否则必须等待:只有缓冲区不空的 ...
- Java Concurrency - Phaser, Controlling phase change in concurrent phased tasks
The Phaser class provides a method that is executed each time the phaser changes the phase. It's the ...
- (Android)View.getHeight或getWidth为0时的一些解决方案
在Android开发过程中,经常需要动态的更改View的大小,有些View的大小可能需要根据其他View的大小来设定,或者你需要得到一些View的大小来进行某项操作,但是有可能你需要在onCreate ...