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. poj1064 二分,注意精度!

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 35269   Accepted: 7513 Des ...

  2. 发送trim值

    发送寄存器和地址上的所有值 uart_send(0xa1); uart_send(*((char code*)0x2001)); uart_send(*((char code*)0x2002)); u ...

  3. Map基本用法

    Map的基本用法 map内部使用的是红黑树,在map内部所有的数据都是有序的 map插入有三种方法: insert(pair<int,string>(i,str)); myMap.inse ...

  4. winPcap_1_开篇

    什么是WinPcap WinPcap是一个基于Win32平台的,用于捕获网络数据包并进行分析的开源库. 因为有些应用程序需要直接访问网络中的数据包.也就是说,那些应用程序需要访问原始数据包,即没有被操 ...

  5. Mysql学习(慕课学习笔记6)修改数据表(上)

    添加单列 Alter table tb1_name ADD[column] col_name column_definition [first | after col_name] 列名称       ...

  6. mysql分库分表总结<转>

    单库单表 单库单表是最常见的数据库设计,例如,有一张用户(user)表放在数据库db中,所有的用户都可以在db库中的user表中查到. 单库多表 随着用户数量的增加,user表的数据量会越来越大,当数 ...

  7. slf4j教程

    slf4j只是一个门面(facet),它不包含具体的实现,而是将一些log4j,java.logging等实现包装成统一的接口.借用下图展示了常用日志文件的关系: 通过上面的图,可以简单的理清关系! ...

  8. Core Data (一)备

    序 恩,用Core Data也有一段时间了.大大小小的坑也都坑过了.重来没有认真的记录一次.这次需要好好的理一理Core Data.就当一次绝好的机会记录下来.也为了自己加深认识. 为什么要用Core ...

  9. 开心菜鸟系列学习笔记------javascript(5)

    一.this的关系    1)全局代码中的this    2)函数代码中的this在函数代码中使用this时很有趣,这种情况很难且会导致很多问题. 这种类型的代码中,this值的首要特点(或许是最主要 ...

  10. poj2484--A Funny♂Game

    题意:n个硬币围成环,每次可以取相邻两个硬币或者一个,不能取者负. Ans:n<=2的时候先手必胜,其他任意情况后手必胜,因为不论我先手取什么,我后手总有中心对称与它配对. #include&l ...