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模拟队列)的更多相关文章

  1. uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列

    题目很简单,给一个队列以及文件的位置,然后一个一个检查,如果第一个是优先级最高的就打印,否则放到队列后面,求所要打印的文件打印需要花费多长时间. 这里我用数组模拟队列实现,考虑到最糟糕的情况,必须把数 ...

  2. uva 12100 Printer Queue

    The only printer in the computer science students' union is experiencing an extremely heavy workload ...

  3. UVa 12100 Printer Queue (习题 5-7)

    传送门:https://uva.onlinejudge.org/external/121/12100.pdf 题意:队列中待打印的任务(1 <= n <= 100)带有优先级(1-9), ...

  4. 12100 Printer Queue(优先队列)

    12100 Printer Queue12 The only printer in the computer science students’ union is experiencing an ex ...

  5. 【UVA - 540】Team Queue (map,队列)

    Team Queue Descriptions: Queues and Priority Queues are data structures which are known to most comp ...

  6. uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟

    挺水的模拟题,刚开始题目看错了,poj竟然过了...无奈.uva果断wa了 搞清题目意思后改了一下,过了uva. 题目要求模拟木块移动: 有n(0<n<25)快block,有5种操作: m ...

  7. UVA 12100 打印队列(STL deque)

    题意: 给定n个优先级打印队列,然后从0开始编号到n-1.出队一个元素,如果他是队列中优先级最高的,打印(耗时一分钟),否则放到队尾(不耗时).给定一个m,求位置m的文件打印的时间. 分析: 用一个p ...

  8. day22 collection 模块 (顺便对比queue也学习了一下队列)

    collection 定义命名元祖,让元祖的每个元素可以通过类似对象属性的方法用".属性"及其方便的取值. 定义可前后拿取值且可迭代的双端队列 定义有顺序的字典 定义有默认值的字典 ...

  9. [python] Queue.Queue vs. collections.deque

    https://stackoverflow.com/questions/717148/queue-queue-vs-collections-deque/717199#717199 Queue,Queu ...

随机推荐

  1. Js/jquery获取当前日期时间及其它操作

    var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-???? ...

  2. 1061 Dating (20 分)

    1061 Dating (20 分) Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh ...

  3. sklearn的BaseEstimator、transformerMixin、ClassifierMixin、RegressorMixin、ClusterMixin介绍

    class sklearn.base.BaseEstimator:为所有的estimators提供基类 方法: __init__() 初始化方法 get_params(deep=True) 获取这个估 ...

  4. Github提交Spark代码

    记录下提交过程,易忘供查询用.内容源自田总的分享. 1)在github上fork一份最新的master代码 2)用社区代码库创建本地仓库 git clone https://github.com/ap ...

  5. Hive启动异常

    [root@host ~]# hivewhich: no hbase in (/root/app/apache-maven-3.5.2/bin:/usr/local/sbin:/usr/local/b ...

  6. viewer 照片查看器

    viewer 照片查看器 效果: api: https://github.com/fengyuanchen/viewerjs#methods npm: npm install viewerjs 使用: ...

  7. CentOS 7 JDK安装

    官网:  http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 1.下载(下图有个错误 ...

  8. 多线程--Java

    多线程: 1.进程和线程 进程是资源分配的最小单位,线程是CPU调度的最小单位. 每个进程的创建都需要系统为其开辟资源内存空间,并发执行的程序在执行过程中分配和管理资源的基本单位,速度和销毁也较慢.进 ...

  9. OpenCV版本下载

    https://sourceforge.net/projects/opencvlibrary/files/opencv-win/

  10. DOM0和D0M2级事件

    1.DOM0级事件:on+事件类型 1.1.在html行内直接绑定, 1.2.在js中绑定 A.DOM0级事件和DOM0级事件相互之间会覆盖,比如以下代码执行后弹出jsDOM0级,js中绑定的事件 覆 ...