Description

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 题目思路:通过队列模拟过程。一个队列q保存输入的数,一个优先队列v。如果q队列的第一个数是v队列中优先级最大的数,就弹出它。如果不是,就把它放在队列后面。一直这样,直到弹出需要的数 代码如下: (借鉴了,他人博客,╮(╯▽╰)╭... C++小白,求不追究责任....)

#include<iostream>
#include<queue>
using namespace std;
int main()
{
int t, n, m, x;
cin>>t;
while(t--)
{
cin>>n>>m;
queue<int>q;
priority_queue<int>v;//优先队列,一声明,就排列,按照优先级从大到小
for(int i=0; i<n; i++)
{
cin>>x;
q.push(x);//将输入的X放到q队列的队尾
v.push(x);//将输入的X放到V队列的队尾
}
while(1)
{
x=q.front();//将q队列的第一个数赋值给x,用于接下来的判断
q.pop();//弹出q队列的第一个元素,先弹出来,接下来再判断

if(m==0)
{
if(x!=v.top())//弹出来的数不是最大优先级

{
m=v.size()-1;//目标的位置变化
q.push(x);//将x(q.front)放到q队列的队尾
}
else//     是最大优先级
break;
}
else
{
m--;
if(x!=v.top()) //
q.push(x);
else
v.pop();//弹出v队列的第一个元素,也就是最大优先级的数
}
}
cout<<n-q.size()<<endl;//n减去q队列剩下的数目就是打印目标需要的时间
}
return 0;
}

/* push(x) 将x压入队列的末端

pop() 弹出队列的第一个元素(队顶元素),注意此函数并不返回任何值

front() 返回第一个元素(队顶元素)

back() 返回最后被压入的元素(队尾元素)

empty() 当队列为空时,返回true

size() 返回队列的长度
top() 返回优先队列中有最高优先级的元素*/

Printer Queue的更多相关文章

  1. POJ 3125 Printer Queue

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

  2. 12100 Printer Queue(优先队列)

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

  3. uva 12100 Printer Queue

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

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

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

  5. J - Printer Queue 优先队列与队列

    来源poj3125 The only printer in the computer science students' union is experiencing an extremely heav ...

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

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

  7. Printer Queue UVA - 12100

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

  8. hdu 1972.Printer Queue 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1972 题目意思:需要模拟打印机打印.打印机里面有一些 job,每个job被赋予1-9的其中一个值,越大 ...

  9. UVa 12100 (模拟) Printer Queue

    用一个队列模拟,还有一个数组cnt记录9个优先级的任务的数量,每次找到当前最大优先级的任务然后出队,并及时更新cnt数组. #include <iostream> #include < ...

随机推荐

  1. hdu 4251 划分树

    思路:裸的划分树 #include<iostream> #include<algorithm> #include<cstdio> #include<cmath ...

  2. poj 2723 2-SAT问题

    思路:二分枚举能开的门的数量,将每次枚举转换成2-SAT问题.这里存在的矛盾是假设有门上a,b两个锁,a锁对应于1号钥匙,而一号钥匙的配对是2号钥匙,b锁对应于3号钥匙,3号的配对是4号钥匙.那么2号 ...

  3. 【转】六年软件测试感悟-从博彦到VMware

    不知不觉已经从事软件测试六年了,2006毕业到进入外包公司外包给微软做软件测试, 到现在加入著名的外企.六年的时间过得真快. 长期的测试工作也让我对软件测试有了比较深入的认识.但是我至今还是一个底层的 ...

  4. 【转】【CDC翻客】移动端App测试实用指南

     译者注:本文从测试人员的角度出发,提出了100多个在测试移动App过程中需要考虑的问题.不管你是测试人员.开发.产品经理或是交互设计师,在进行移动App开发时,这些问题都很有参考价值.我和Queen ...

  5. JavaScript 三种绑定事件方式之间的区别

    JavaScript三种绑定事件的方式: 1. <div id="btn" onclick="clickone()"></div> // ...

  6. JMS - Message

    一条 JMS 消息包含三个部分:消息头.消息属性和消息体. 消息头 消息头提供了和消息有关的元数据,它描述了消息有谁创建.何时创建.数据的有效长度等信息.消息头还包含了描述消息目的地(主题或队列)的路 ...

  7. HDOJ2016数据的交换输出

    数据的交换输出 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  8. MTKLogger日志记录打开

    MTKLogger在 拨号界面用*#446633#(不同手机可能设置不一样) 向左滑动到Log and Debugging下面有个MTKLogger,点进去 点击右上角进入设置,打开MobileLog ...

  9. 安装和启动mongodb数据库

    参考链接:http://www.fkblog.org/blog569 参考链接:http://www.cnblogs.com/linjiqin/p/3192159.html

  10. Cocos2d-x中停止播放背景音乐

    停止背景音乐播放代码放置到什么地方比较适合呢?例如:在HelloWorld场景中,主要代码如下: bool HelloWorld::init() { return true; } void Hello ...