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.
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 < ... 
随机推荐
- css笔记10:多个id选择器/类选择器包含相同部分问题的探讨
			有些时候,我们可以将多个class选择器或者id选择器,html选择器的共同部分提取出来,写在一起,这样的好处是是可以简化css文件 1.首先我们先看一段代码.css,如下: @charset &qu ... 
- javaweb学习总结十七(web应用组织结构、web.xml作用以及配置虚拟主机搭建网站)
			一:web应用组织结构 1:web应用组成结构 2:安装web组成机构手动创建一个web应用程序目录 a:在webapps下创建目录web b:在web目录下创建html.jsp.css.js.WEB ... 
- javaweb学习总结十三(dom4j方式对XML文档进行解析以及Xpath的使用)
			一:dom4j方式介绍 对于xml的解析总共有三种 1:jaxp方式,是sun公司开发的,分为sax方式和dom方式 2:jdom方式,后来其中部分人员参与开发dom4j 3:dom4j方式,是现在企 ... 
- [转]bat批处理实现TXT文本合并
			本文转自:http://quanhuaming.blog.163.com/blog/static/1405693672010210101124905/ 有朋友问是否有可以合并TXT文本文件的软件,于是 ... 
- ionic获取事件中的对象
			ng-click="submit1($event, 'argsTest’)" $scope.submit1=function(event, args){ var target = ... 
- CF Watto and Mechanism (字典树+深搜)
			Watto and Mechanism time limit per test 3 seconds memory limit per test 256 megabytes input standard ... 
- 【BZOJ1212】[HNOI2004]L语言 Trie树
			[BZOJ1212][HNOI2004]L语言 Description 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是由若干小写字母构 ... 
- Java Concurrency - ScheduledThreadPoolExecutor
			The Executor framework provides the ThreadPoolExecutor class to execute Callable and Runnable tasks ... 
- Linux 命令 - umask: 显示或设置文件模式掩码值
			umask 命令控制着创建文件时指定给文件的默认权限.它使用八进制表示法从文件模式属性中删除一个位掩码. 参见下面的例子: [huey@huey-K42JE cmdline]$ rm -f foo.t ... 
- ASP判断当前页面上是否有参数ID传递过来
			遇到了一个这样的ASP问题: 在当前页面上判断,是否有参数ID传递过来? 如果没有,显示“没有参数传递过来”. 如果有传递,但值为空,显示“存在参数,但参数为空” <% if (request( ... 
