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, then move 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 important muffin 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 simplify matters, 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 ≤mn− 1). The first position in the queue is number 0, the second is number 1, and so on.

• One line with 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

HINT

有两个队列,一个普通的队列,一个优先队列。只要优先队列和普通队列的头元素相同就出队,记录时间。否则添加到队尾。使用一个整数记录关注内容的位置,当其为0并且可以出队就结束程序。

Accepted

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std; int main()
{
int num, m, n,t;
cin >> num;
while (num--) {
cin >> m>>n;
queue<int>q;
priority_queue<int>pq;
while (m--) {
cin >> t;
q.push(t);pq.push(t);
}
int time = 0;
while (1) {
if (q.front() == pq.top()) {
time++;
q.pop();pq.pop();
if (n == 0) {
cout << time << endl;
break;
}
n--;
}
else {
n = (n == 0) ? q.size() - 1 : n - 1;
q.push(q.front());q.pop();
}
} }
}

Printer Queue UVA - 12100的更多相关文章

  1. uva 12100 Printer Queue

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

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

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

  3. 12100 Printer Queue(优先队列)

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

  4. Printer Queue

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

  5. POJ 3125 Printer Queue

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

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

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

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

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

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

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

  9. UVa 12100 (模拟) Printer Queue

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

随机推荐

  1. SecureCRT无法登陆ubuntu问题解决的方法(亲测有效)

    最近在虚拟机安装了几个ubuntu系统玩耍,然后想着用SecureCRT在Windows本地连接但是怎么也连接不上!!!如下,这只是示意图,ip地址是瞎编的,但是情况完全相同,期间尝试过让linux和 ...

  2. Lambda 表达式简介

    0.预备知识 函数式接口:只包含一个抽象方法的接口. 内部类:静态.成员内部类 局部内部类 匿名内部类 1.代码 1 /** 2 * 函数式编程: 3 * lambda表达式前提: 4 * 必须是函数 ...

  3. 力扣208. 实现 Trie (前缀树)

    原题 以下是我的代码,就是简单的字符串操作,可以ac但背离了题意,我之前没接触过Trie 1 class Trie: 2 3 def __init__(self): 4 ""&qu ...

  4. Debian中的NVIDIA显卡驱动安装——超简单,一行命令

    其实Debian的non-free固件中包含NVIDIA的显卡驱动,所以没必要在官网下run包一步一步来 sudo apt install nvidia-settings 安装时会提示与X冲突,没关系 ...

  5. 趣谈 DHCP 协议,有点意思。

    计算机网络我也连载了很多篇了,大家可以在我的公众号「程序员cxuan」 或者我的 github 系统学习. 计算机网络第一篇,聊一聊网络基础 :计算机网络基础知识总结 计算机网络第二篇,聊一聊 TCP ...

  6. docker+compose+nginx+php

    Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化. 我用docker做什么? 快速搭建开发所需环境,测试实验新 ...

  7. 13、Script file 'E:\Anaconda Distribution\Anaconda\Scripts\pip-script.py' is not present.

    pip-script.py文件缺失问题 问题: Script file 'E:\Anaconda Distribution\Anaconda\Scripts\pip-script.py' is not ...

  8. Android学习之启动活动的最佳写法

    •开始热身 通过之前的学习,我们现在可以很容易的启动一个活动: 首先通过 Intent 构造出当前的 "意图",然后调用  startActivity()  方法将活动启动起来: ...

  9. PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...

  10. 基于Hive进行数仓建设的资源元数据信息统计:Hive篇

    在数据仓库建设中,元数据管理是非常重要的环节之一.根据Kimball的数据仓库理论,可以将元数据分为这三类: 技术元数据,如表的存储结构结构.文件的路径 业务元数据,如血缘关系.业务的归属 过程元数据 ...