题目,排队打印问题

Input Format

One line with a positive integer: the number of test cases (at most 20). 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 ≤ 50) 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 Format

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.

Input Sample

3
1 0
5
4 2
1 2 3 4
6 0
1 1 9 1 1 1

Output Sample

1
2
5
中文说明,第一行为测试样例组数,下面n组中每组2行,1行的地一个为有几个人在排队,第二个为要求的第几个人的时间(从0数起),下面一行每个数字代表一个人而数字代表这个人的任务的重要级别
题目是这样的,队头的人如果不是队列中级别最高的,则到队尾继续排队(此过程不消耗时间),当队头的人的级别是队列中最高(大于等于)时,则这个人出队,其他人消耗时间+1.
代码如下
     #include <iostream>
#include <queue>
using namespace std; int main() {
int z;
cin >> z;
while (z--) {
int n, m, pri;
int count = ;
priority_queue<int> pri_queue;
queue<int> cou_queue;
queue<int> ini_queue;
cin >> n >> m;
for (int i = ; i < n; i++) {
cin >> pri;
cou_queue.push(i);
ini_queue.push(pri);
pri_queue.push(pri);
}
while () {
while (ini_queue.front() != pri_queue.top()) {
int temp = ini_queue.front();
ini_queue.pop();
ini_queue.push(temp);
temp = cou_queue.front();
cou_queue.pop();
cou_queue.push(temp);
}
count++;
if (cou_queue.front() == m) {
cout << count << endl;
break;
} else {
pri_queue.pop();
cou_queue.pop();
ini_queue.pop();
}
}
}
return ;
}

本题学会了优先队列的使用,优先队列出队的都是队列中重要级别最大的那个人,但单独使用优先队列不足以解决问题,要需要使用2个普通队列的结合,一个记录标号,一个记录优先级别.当普通队列的对头和优先队列的队头一样是,则出对,看代码即可理解

priority queue优先队列初次使用的更多相关文章

  1. Algorithms - Priority Queue - 优先队列

    Priority queue - 优先队列 相关概念 Priority queue优先队列是一种用来维护由一组元素构成的集合S的数据结构, 其中的每一种元素都有一个相关的值,称为关键字(key). 一 ...

  2. Priority Queue(优先队列)

    今天早上起来完成了一个完整的基于二叉堆实现的优先队列,其中包含最小优先和最大优先队列. 上篇说了优先队列的特性,通过建堆和堆排序操作,我们就已经看到了这种数据结构中的数据具有某种优先级别,要么非根节点 ...

  3. 优先队列Priority Queue和堆Heap

    对COMP20003中的Priority queue部分进行总结.图片来自于COMP20003 queue队列,顾名思义特点先进先出 priority queue优先队列,出来的顺序按照优先级prio ...

  4. STL之heap与优先级队列Priority Queue详解

    一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...

  5. 优先队列(Priority Queue)

    优先队列(Priority Queue) A priority queue must at least support the following operations: insert_with_pr ...

  6. c++ STL:队列queue、优先队列priority queue 的使用

    说明:本文全文转载而来,原文链接:http://www.cppblog.com/wanghaiguang/archive/2012/06/05/177644.html C++ Queues(队列) C ...

  7. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅴ

    命题Q.对于一个含有N个元素的基于堆叠优先队列,插入元素操作只需要不超过(lgN + 1)次比较,删除最大元素的操作需要不超过2lgN次比较. 证明.由命题P可知,两种操作都需要在根节点和堆底之间移动 ...

  8. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅳ

    2.4.4 堆的算法 我们用长度为 N + 1的私有数组pq[]来表示一个大小为N的堆,我们不会使用pq[0],堆元素放在pq[1]至pq[N]中.在排序算法中,我们只能通过私有辅助函数less()和 ...

  9. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅰ

    许多应用程序都需要处理有序的元素,但不一定要求他们全部有序,或者是不一定要以此就将他们排序.很多情况下我们会手机一些元素,处理当前键值最大的元素,然后再收集更多的元素,再处理当前键值最大的元素.如此这 ...

随机推荐

  1. Java测试(一)

    关于while和do-while循环,下列说法正确的是  A 两种循环除了格式不同外,功能完全相同  B 与do-while语句不通的是,while语句的循环至少执行一次  C do-while语句首 ...

  2. window7 安装sass和compass

    官网有详细的介绍,但是安装时候还是出现了一些小问题. 首先下载Rudy,然后根据提示勾选加入环境变量,由于第一次使用,我就选择了" msys2 base installlation" ...

  3. ASP.NET MVC编程——错误处理与日记

    ASP.NET MVC的错误处理应考虑到这几个方面:模型绑定期间发生的错误,未能路由到指定操作,针对控制器的错误处理.使用配置文件可以帮助我们处理异常,但是不够灵活和全面:使用HandleErrorA ...

  4. 配置SecureCRT以SSH公钥方式登录服务器

    为了更方便管理服务器,CRT一个功能非常方便.那就是可以使用私钥认证通过后直接连接,而且对于密码保护比较安全一点. 首先配置ssh Port 32812Protocol 2UseDNS noPubke ...

  5. windows上SSH服务连接远程主机失败

    putty连接出现错误提示"Network error:connection refused",xshell连接出现错误提示"connection failed:(por ...

  6. java排序算法(六):直接插入排序

    java排序算法(六):直接插入排序 直接插入排序的基本操作就是将待的数据元素按其关键字的大小插入到前面的有序序列中 直接插入排序时间效率并不高,如果在最坏的情况下,所有元素的比较次数的总和为(0+1 ...

  7. 自定义ArrayList

    自定义实现ArrayList很简单,只需要明白下面几点 1.ArrayList 底层是由数组组成的 2.数组有容量限制,但是ArrayList并没有(实际上也有,Integer.MAX_VALUE). ...

  8. Struts存取数据

    ValueStack举例分析: Action存 Jsp页面取,用于数据展示 存数据三种方式总结 存数据->map 或 root 展示数据->Strusts标签   这个玩意用着很舒服,能让 ...

  9. web面试题

    1.你做的页面在哪些流览器测试过?这些浏览器的内核分别是什么? Ie(Ie内核) 火狐(Gecko) 谷歌(webkit) opear(Presto)

  10. Linux下ftp和ssh详解

    学习了几天Linux下ftp和ssh的搭建和使用,故记录一下.学习ftp和ssh的主要目的是为了连接远程主机,并且进行文件传输.废话不多说,直接开讲! ftp服务器 1. 环境搭建 本人的系统是Arc ...