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. SQL 字段查找

    select [name] from sysobjects where [id] in (select [id] from syscolumns where [name]='a1') SQL 2005 ...

  2. phpstorm使用教程

    phpstorm包含了webstorm的全部功能,更能够支持php代码.PhpStorm是一个轻量级且便捷的PHP IDE,其旨在提供用户效率,可深刻理解用户的编码,提供智能代码补全,快速导航以及即时 ...

  3. securecrt8注册码

    securecrt8注册码,两个可用 Name:meisiCompany:TEAM ZWTSerial Number:03-14-367662License Key:ACCFAX R9FHJ7 QZV ...

  4. Activity服务类-1 DynamicBpmnService服务类

    这个服务是5.19版本后新增的一个服务,和RepositoryService的作用相似,都是与流程定义有关,但是却完全不同.从名字上来看是动态的BPMN服务,看里面的方法都是改变流程的相关属性.这个方 ...

  5. style css

    Title 语文 用户名 用户名 数学 英语 <!DOCTYPE html><html lang="en"><head> <meta ch ...

  6. setdeamon 设置 线程为守护线程, (lock线程锁, BoundedSemaphore,rlock递归锁 ) 三种锁

    1.setdeamon 当主程序执行完时,子程序自动被销毁 ,内存自动被收回 例一: import threading, time def run(n): print('run %s'%n) time ...

  7. python中关键字的总结

    python中各种关键字的总结:用表格形式,解释关键字符号的作用和案例说明                    关键字                                         ...

  8. ABAP-ITS Mobile

    ITS Mobile是14年开发EWM项目时用到的技术方案,本文主要记录下ITS Mobile的整个实现过程. 1.ITS Mobile介绍 ITS Mobile:Internet Transacti ...

  9. objective c, property, copy

    @property (copy) nsmutablearray *array -copy, as implemented by mutable Cocoa classes, always return ...

  10. centoros 环境安装

    1. nginx rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.no ...