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. 吸血鬼数字算法参考 -- javascript版本

    // 吸血鬼数字 java编程思想 第四章 75页 练习10 for (var i = 10; i <= 99; i++) { for (var j = i + 1; j < 99; j+ ...

  2. 16 Socket通信(简单例子)

      服务端代码: import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.util.Da ...

  3. Hadoop集群日常运维

    (一)备份namenode的元数据 namenode中的元数据非常重要,如丢失或者损坏,则整个系统无法使用.因此应该经常对元数据进行备份,最好是异地备份. 1.将元数据复制到远程站点 (1)以下代码将 ...

  4. sql 更新一列为行号

    update u_menu set issort=t1.rowId from (  --select * from  --(   select cmenu_id,ROW_NUMBER() over(O ...

  5. [转]jQuery插件开发精品教程,让你的jQuery提升一个台阶

    原文链接:http://www.cnblogs.com/Wayou/p/jquery_plugin_tutorial.html 要说jQuery 最成功的地方,我认为是它的可扩展性吸引了众多开发者为其 ...

  6. python之简单入门01

     python简单的介绍使用: 一.个人感觉写Python程序,最好用的工具就是pycharm了,自动补全功能可以满足大多数不太喜欢记忆的人群: 安装pycharm之前应该先安装python解释器,目 ...

  7. 小试牛刀——爬topit.me的图片,附github简易上传教程

    接触了scrapy ,发现爬虫效率高了许多,借鉴大神们的文章,做了一个爬虫练练手: 我的环境是:Ubuntu14.04 + python 2.7 + scrapy 0.24 目标 topit.me 一 ...

  8. DataTables获取表单输入框数据

    $(document).ready(function() { var table = $('#example').DataTable(); $('button').click(function() { ...

  9. ByteArrayOutputStream的用法

    ByteArrayOutputStream类是在创建它的实例时,程序内部创建一个byte型别数组的缓冲区,然后利用ByteArrayOutputStream和ByteArrayInputStream的 ...

  10. 图片以BLOB存储在后台数据库中,Android客户端要进行读取显示

    解决方法: 1:在后台以InputStream的方式将图片从数据库中读出: public static InputStream getPicInputStream(){ String id = &qu ...