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
5
 
用队列做,m变化来记录队列中我的job的位置,开始代码超时
#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的更多相关文章

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

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

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

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

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

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

  4. 12100 Printer Queue(优先队列)

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

  5. 【UVA】12100 Printer Queue(STL队列&优先队列)

    题目 题目     分析 练习STL     代码 #include <bits/stdc++.h> using namespace std; int main() { int t; sc ...

  6. Printer Queue UVA - 12100

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

  7. Printer Queue

    Description The only printer in the computer science students' union is experiencing an extremely he ...

  8. POJ 3125 Printer Queue

    题目: Description The only printer in the computer science students' union is experiencing an extremel ...

  9. [刷题]算法竞赛入门经典(第2版) 5-7/UVa12100 - Printer Queue

    题意:一堆文件但只有一个打印机,按优先级与排队顺序进行打印.也就是在一个可以插队的的队列里,问你何时可以打印到.至于这个插队啊,题目说"Of course, those annoying t ...

随机推荐

  1. 8 fastJson的使用

    Fastjson介绍 Fastjson是一个Java语言编写的JSON处理器,由阿里巴巴公司开发. 1.遵循http://json.org标准,为其官方网站收录的参考实现之一. 2.功能qiang打, ...

  2. angularjs如何在ng-repeat过程中控制字符串长度超过指定长度后面内容以省略号显示

    angular.module('ng').filter('cut', function () { return function (value, wordwise, max, tail) { if ( ...

  3. 转载收藏之用 - 微信公众平台开发教程(七):解决用户上下文(Session)问题

    从这篇文章中我们已经了解了微信公众平台消息传递的方式,这种方式有一个先天的缺陷:不同用户的请求都来自同一个微信服务器,这使得常规的Session无法使用(始终面对同一个请求对象,况且还有对方服务器Co ...

  4. gridview两列数据的互换

    如下图所示: GridView绑定数据的时候,若ReName列里面有数据,则显示ReName列里面的数据,如果没有数据,则显示Name列里面的数据.Name和ReName是数据表里面的两个字段< ...

  5. bzoj1655 [Usaco2006 Jan] Dollar Dayz 奶牛商店

    Description Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of to ...

  6. SICP 练习 1.3

    (define (sum a b) (+ a b)) (define (sum-two a b c) ( cond ((and (> (sum a b) (sum a c)) (> (su ...

  7. IDX爱定客 | 氪加

    IDX爱定客 | 氪加 个性化定制鞋网站,在线定制只需三分钟

  8. hdu 5427 A problem of sorting(字符排序)

    Problem Description There are many people's name and birth in a list.Your task is to print the name ...

  9. 【多线程】--生产者消费者模式--Lock版本

    在JDK1.5发布后,提供了Synchronized的更优解决方案:Lock 和 Condition 我们使用这些新知识,来改进例子:[多线程]--生产者消费者模式--Synchronized版本 改 ...

  10. python学习之路-2 初识python数据类型

    数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647 在64位系统上,整数的位数为64位,取值范围为-2** ...