题目,排队打印问题

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实现)

    前中后序遍历递归实现+层序遍历: 树的结点类代码: public class TreeNode<Value extends Comparable<? super Value>> ...

  2. CSS(CSS3)选择器(1)

    这篇文章主要用于存储CSS以及CSS3的选择器部分知识,以便日后查阅及记忆. 该内容分为两部分,第一部分为css选择器的一些基本知识.第二部分为CSS3新增加的选择器. 在开始之前,先简单介绍一下选择 ...

  3. apache tomcat 安装

    1.安装jdk (java development kit) jdk下载 http://download.oracle.com/otn-pub/java/jdk tar -zxvf jdk-8u121 ...

  4. iOS 消息发送与转发详解

    Objective-C 是一门动态语言,它将很多静态语言在编译和链接时期做的事情,放到了运行时来处理.之所以能具备这种特性,离不开 Runtime 这个库.Runtime 很好的解决了如何在运行时期找 ...

  5. 『练手』手写一个独立Json算法 JsonHelper

    背景: > 一直使用 Newtonsoft.Json.dll 也算挺稳定的. > 但这个框架也挺闹心的: > 1.影响编译失败:https://www.cnblogs.com/zih ...

  6. 【IntelliJ IDEA】使用idea解决新建jsp文件而找不到jsp文件模版的新建选项

    使用idea解决新建jsp文件而找不到jsp文件模版的新建选项,这样每次创建一个新的jsp文件岂不是很耗时间? 解决办法: 就是要让idea知道你需要在这个目录下创建jsp文件 左上角,file中点击 ...

  7. 通过运行一个tomcat容器来记录下初学docker常用的几个命令---容器篇

    1.查看容器列表 显示正在运行的容器: [root@localhost HMK]# docker ps 显示所有容器,包括未运行的: [root@localhost HMK]# docker ps - ...

  8. Beta Scrum Day 7

    听说

  9. Beta阶段报告

    Beta版测试报告 1. 在测试过程中总共发现了多少Bug?每个类别的Bug分别为多少个? BUG名 修复的BUG 不能重现的BUG 非BUG 没能力修复的BUG 下个版本修复 url乱码 √ 手机端 ...

  10. 敏捷开发冲刺--day3

    1 团队介绍 团队组成: PM:齐爽爽(258) 小组成员:马帅(248),何健(267),蔡凯峰(285)  Git链接:https://github.com/WHUSE2017/C-team 2 ...