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 < ...
随机推荐
- SQL Server :事务和锁
1.事务 事务概念:全部执行或全部不执行的一条或者多条语句的组合 例子说明:到银行里转账,将一个账户(Tom)里的100元钱转到另一个账户(Jake) update table money=money ...
- MSP430常见问题之通信类
Q1: 430 串口中,有个R/D 控制线,在接收上位机的数据,但本身的数据有无发送完毕不知道啊,什么时候才可置低R/d 位来接收数据啊?好像430 没有发送完中断标志A1:字节主动发送,一般都能发出 ...
- 转:Linux网络IO并行化技术概览
转:http://codinginet.com/articles/view/201605-linux_net_parallel?simple=1&from=timeline&isapp ...
- Ext.Slider的试用小记
最近需要做一个滑动条,动态改变地图动画播放的速度.实在没有自己用js写一个的那个水平,于是找到了Ext.Slider,ExtJS的版本是3.3.0. new Ext.Slider({ id: 'pla ...
- c# 与 PHP中 SHA1加密结果不同解决方法
那天在调试API的时候,发现用c#写的SHA1加密出来的结果和PHP中sha1()出来的不一样,找了半天的原因后来才弄出来 在调试微信接口的时候大多的帮助文档都是提供的是PHP的方法,所以在.net中 ...
- C#中显/隐式实现接口及其访问方法
原贴地址: http://www.cnblogs.com/dudu837/archive/2009/12/07/1618663.html 在实现接口的时候,VS提供了两个菜单,一个是"实现接 ...
- HDOJ2014青年歌手大奖赛_评委会打分
青年歌手大奖赛_评委会打分 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Agile.Net 组件式开发平台 - 数据报表组件
Agile.Report.dll 文件为平台数据报表支持库,基于FasstReport.Net扩展重写,提供了非常强大的自定义报表的功能使开发者为应用程序快速有效地生成报表.报表类库提供了创建报表所需 ...
- sql server 2008 查询语句的红色波浪线
在 Microsoft sql server management studio 里点击“编辑”——“IntelliSense”——“刷新本地缓存” 就会发现红色波浪线没了(前提是你的代码没错)
- Could not delete folder on Win7
I had the same problem on Win 7 and this worked for me in command prompt. Solution 1: RD/S pathname ...