uva 12100 Printer Queue
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
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std; queue<int>job;
int p[+]; int main()
{
int T;
cin >> T;
while(T--){
int n ,m,time = ;
while(job.size())job.pop(); //清空队列 cin>>n>>m;
for(int i = ;i < n; i++){
cin >> p[i];
job.push(p[i]);
} sort(p,p+n); for(int i = ; ; i++){ if(p[n-] > job.front()){
job.push(job.front());
job.pop();
if(m == )m = n-;
else m--;
} else{
job.pop();
n--;
time++;
if(m == )break;
else m--;
}
}
cout<<time<<endl; }
//system("pause");
return ;
}
将队列定义在主函数中,没有清空队列这一操作就不超时了
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std; int p[+]; int main()
{
int T;
cin >> T;
while(T--){
int n ,m,time = ;
queue<int>job; cin>>n>>m;
for(int i = ;i < n; i++){
cin >> p[i];
job.push(p[i]);
} sort(p,p+n); for(int i = ; ; i++){ if(p[n-] > job.front()){
job.push(job.front());
job.pop();
if(m == )m = n-;
else m--;
} else{
job.pop();
n--;
time++;
if(m == )break;
else m--;
}
}
cout<<time<<endl; }
//system("pause");
return ;
}
uva 12100 Printer Queue的更多相关文章
- UVa 12100 Printer Queue(queue或者vector模拟队列)
The only printer in the computer science students' union is experiencing an extremely heavy workload ...
- UVa 12100 Printer Queue (习题 5-7)
传送门:https://uva.onlinejudge.org/external/121/12100.pdf 题意:队列中待打印的任务(1 <= n <= 100)带有优先级(1-9), ...
- uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列
题目很简单,给一个队列以及文件的位置,然后一个一个检查,如果第一个是优先级最高的就打印,否则放到队列后面,求所要打印的文件打印需要花费多长时间. 这里我用数组模拟队列实现,考虑到最糟糕的情况,必须把数 ...
- 12100 Printer Queue(优先队列)
12100 Printer Queue12 The only printer in the computer science students’ union is experiencing an ex ...
- 【UVA】12100 Printer Queue(STL队列&优先队列)
题目 题目 分析 练习STL 代码 #include <bits/stdc++.h> using namespace std; int main() { int t; sc ...
- Printer Queue UVA - 12100
The only printer in the computer science students' union is experiencing an extremely heavy workload ...
- Printer Queue
Description The only printer in the computer science students' union is experiencing an extremely he ...
- POJ 3125 Printer Queue
题目: Description The only printer in the computer science students' union is experiencing an extremel ...
- [刷题]算法竞赛入门经典(第2版) 5-7/UVa12100 - Printer Queue
题意:一堆文件但只有一个打印机,按优先级与排队顺序进行打印.也就是在一个可以插队的的队列里,问你何时可以打印到.至于这个插队啊,题目说"Of course, those annoying t ...
随机推荐
- LeetCode-Divdend two Integers
题目: Divide two integers without using multiplication, division and mod operator. 思路分析 二分法.将除数不断增倍,而结 ...
- 【译】神经网络与深度学习 Ch1-Section0
用神经网络识别手写数字 人类的视觉系统是是大自然的奇迹.考虑下面手写数字序列: 大多数人能够轻易地是识别出是504192.在我们大脑的每个半球都有一个基础的皮质,这就是我们熟知的V1区,它包含了14亿 ...
- wind7下搭建ftp服务器
一.首先在本地机器上创建一个用户!这些用户是用来登录到FTP的!我的电脑右键->控制面板->管理工具->计算机管理->本地用户和组->用户->“右键”新建用户-&g ...
- js页面加载进度条
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- C++ 使用cl命令编辑时发生的问题收录
1.cl不是内部或这外部命令,也不是可运行的程序或批处理文件. 这个问题的出现的原因是因为系统找不到cl命令处理程序,无法识别,解决办法: 首先在C盘查找cl.exe,可能会查到多个,先选择第一个,将 ...
- python学习第一天 -安装配置及其输入输出
Python, 是一种面向对象.解释型计算机程序设计语言. python适合领域: 1.Web网络和各种网络服务 2.系统工具和脚本 3.作为“胶水”语言把其他语言开发的模块包装起来方便使用 pyth ...
- 布尔值(Boolean values)
布尔值是特殊的整数. 尽管布尔值由常量 True 和 False 来表示, 如果将布尔值放到一 个数值上下文环境中(比方将 True 与一个数字相加), True 会被当成整数值 1, 而 False ...
- 了不起的分支和循环03 - 零基础入门学习Python009
了不起的分支和循环03 让编程改变世界 Change the world by program while循环 说完了分支我们来说说循环,标准的while循环语法我们已经可以熟悉掌握了. 这里我们就简 ...
- jquery中checkbox全选失效的解决方法
这篇文章主要介绍了jquery中checkbox全选失效的解决方法,需要的朋友可以参考下 如果你使用jQuery 1.6 ,代码if ( $(elem).attr(“checked”) ),将 ...
- Cmake中的find_package功能
find_package其实在windows下扮演的角色并不是很重要.在Unix下就非常重要了,find_package可以根据cmake内置的.cmake的脚本去找相应的库的模块,当然,内建了很多库 ...