UVa 12100 Printer Queue(queue或者vector模拟队列)

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 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
3
1 0
5
4 2
1 2 3 4
6 0
1 1 9 1 1 1
Sample Output
1
2
题意
给你n个任务的优先度(9最大),和需要的第m个位子(0开始),求打印到第m个位子的时间
打印工作方式:首先从队列头取出1个J,如果队列里有更大的,就把J放到队列最后
否则打印J
题解1
一开始没想到用queue如何做,问题在于怎么判断后面有比他大的
于是干脆用vector模拟一下队列
代码1
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pi;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T,n,m,w;
cin>>T;
while(T--)
{
vector<pi> vec;
cin>>n>>m;
for(int i=;i<n;i++)
{
cin>>w;
vec.push_back(pi(w,i));
}
int time=;
while()
{
int pr=vec[].first,pos=vec[].second,F=;
for(int i=;i<vec.size();i++)
{
if(vec[i].first>pr)
{
F=;break;
}
}
vec.erase(vec.begin());
if(F)
{
time++;
if(pos==m)
break;
}
else
vec.push_back(pi(pr,pos));
}
cout<<time<<endl;
}
return ;
}
题解2
后来想了个queue的,开个Cnt[10]数组用来存数字就可以解决了
代码2
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pi;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T,n,m,w;
cin>>T;
while(T--)
{
int Cnt[]={};
queue<pi> qu;
cin>>n>>m;
for(int i=;i<n;i++)
{
cin>>w;
Cnt[w]++;
qu.push(pi(w,i));
}
int time=;
while()
{
int pr=qu.front().first,pos=qu.front().second,F=;
qu.pop();
for(int i=;i>pr;i--)
if(Cnt[i])
F=;
if(F)
{
time++;
Cnt[pr]--;
if(pos==m)
break;
}
else
qu.push(pi(pr,pos));
}
cout<<time<<endl;
}
return ;
}
UVa 12100 Printer Queue(queue或者vector模拟队列)的更多相关文章
- uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列
题目很简单,给一个队列以及文件的位置,然后一个一个检查,如果第一个是优先级最高的就打印,否则放到队列后面,求所要打印的文件打印需要花费多长时间. 这里我用数组模拟队列实现,考虑到最糟糕的情况,必须把数 ...
- uva 12100 Printer Queue
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), ...
- 12100 Printer Queue(优先队列)
12100 Printer Queue12 The only printer in the computer science students’ union is experiencing an ex ...
- 【UVA - 540】Team Queue (map,队列)
Team Queue Descriptions: Queues and Priority Queues are data structures which are known to most comp ...
- uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
挺水的模拟题,刚开始题目看错了,poj竟然过了...无奈.uva果断wa了 搞清题目意思后改了一下,过了uva. 题目要求模拟木块移动: 有n(0<n<25)快block,有5种操作: m ...
- UVA 12100 打印队列(STL deque)
题意: 给定n个优先级打印队列,然后从0开始编号到n-1.出队一个元素,如果他是队列中优先级最高的,打印(耗时一分钟),否则放到队尾(不耗时).给定一个m,求位置m的文件打印的时间. 分析: 用一个p ...
- day22 collection 模块 (顺便对比queue也学习了一下队列)
collection 定义命名元祖,让元祖的每个元素可以通过类似对象属性的方法用".属性"及其方便的取值. 定义可前后拿取值且可迭代的双端队列 定义有顺序的字典 定义有默认值的字典 ...
- [python] Queue.Queue vs. collections.deque
https://stackoverflow.com/questions/717148/queue-queue-vs-collections-deque/717199#717199 Queue,Queu ...
随机推荐
- 3-4 1449 web view
1.app类型 不同类型的应用 区别 native app 纯原声app,Android用Java些,iOS用object c写 hybrid app 套用原声应用的外壳,既有原生的UI页面,又通过内 ...
- https的加密解密是怎么写的?
原文转载至:http://blog.csdn.net/aqiangsz/article/details/53611665 文章中有些不对的地方,比如用证书对改随机码进行加密,这个是不对,证书本身并没有 ...
- Java中的volatile关键字为什么不是不具有原子性
Java中long赋值不是原子操作,因为先写32位,再写后32位,分两步操作,而AtomicLong赋值是原子操作,为什么?为什么volatile能替代简单的锁,却不能保证原子性?这里面涉及volat ...
- STL容器能力一览表和各个容器操作函数异常保证
STL容器能力一览表 Vector Deque List Set Multiset map Multimap 典型内部 结构 dynamic array Array of arrays Doubly ...
- Nop权限的使用
1.首先后台Admin--->siteMap中添加: <siteMapNode SystemName="我是系统名" nopResource="显示的中文名称 ...
- smyfony2 增删改查
1查询和修改
- docker学习笔记 参考
https://www.cnblogs.com/YDDMAX/p/6045079.html 参考此人播客:docker 分类 http://www.cnblogs.com/51kata/categor ...
- 4.struts2的配置文件优先级
转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 在struts2中一些配置(比如常量)可以同时在struts-default ...
- 如何使用Python快速制作可视化报表----pyecharts
如何使用Python快速制作可视化报表 数据可视化能力已经越来越成为各岗位的基础技能.领英的数据报告显示,数据可视化技能在2017年中国最热门技能中排名第一. 就数据分析而言,可视化探索几乎是你正 ...
- wildFly(Jboss as)入门
目录 简介 安装 使用 简介 JBoss AS 从8版本起名为wildfly.JBoss是纯Java的EJB(企业JavaBean)服务器. JBoss As 由 Redhat出品的开源免费服务器,采 ...