priority queue优先队列初次使用
题目,排队打印问题
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优先队列初次使用的更多相关文章
- Algorithms - Priority Queue - 优先队列
Priority queue - 优先队列 相关概念 Priority queue优先队列是一种用来维护由一组元素构成的集合S的数据结构, 其中的每一种元素都有一个相关的值,称为关键字(key). 一 ...
- Priority Queue(优先队列)
今天早上起来完成了一个完整的基于二叉堆实现的优先队列,其中包含最小优先和最大优先队列. 上篇说了优先队列的特性,通过建堆和堆排序操作,我们就已经看到了这种数据结构中的数据具有某种优先级别,要么非根节点 ...
- 优先队列Priority Queue和堆Heap
对COMP20003中的Priority queue部分进行总结.图片来自于COMP20003 queue队列,顾名思义特点先进先出 priority queue优先队列,出来的顺序按照优先级prio ...
- STL之heap与优先级队列Priority Queue详解
一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...
- 优先队列(Priority Queue)
优先队列(Priority Queue) A priority queue must at least support the following operations: insert_with_pr ...
- c++ STL:队列queue、优先队列priority queue 的使用
说明:本文全文转载而来,原文链接:http://www.cppblog.com/wanghaiguang/archive/2012/06/05/177644.html C++ Queues(队列) C ...
- 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅴ
命题Q.对于一个含有N个元素的基于堆叠优先队列,插入元素操作只需要不超过(lgN + 1)次比较,删除最大元素的操作需要不超过2lgN次比较. 证明.由命题P可知,两种操作都需要在根节点和堆底之间移动 ...
- 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅳ
2.4.4 堆的算法 我们用长度为 N + 1的私有数组pq[]来表示一个大小为N的堆,我们不会使用pq[0],堆元素放在pq[1]至pq[N]中.在排序算法中,我们只能通过私有辅助函数less()和 ...
- 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅰ
许多应用程序都需要处理有序的元素,但不一定要求他们全部有序,或者是不一定要以此就将他们排序.很多情况下我们会手机一些元素,处理当前键值最大的元素,然后再收集更多的元素,再处理当前键值最大的元素.如此这 ...
随机推荐
- 基于 Hexo + GitHub Pages 搭建个人博客(一)
前言:我的博客写作之路 15 年刚上大学,第一次接触公众号,就萌生了创建一个公众号写点东西,但最终不了了之. 很快到了 16 年,开始接触网上各大博客网站,接触最多的当属 CSDN,萌生了注册一个博客 ...
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListene解决办法
照着以前写的项目搭一个框架,项目用到的是ssm,spring+spring mvc+mybatis,由Eclipse转战IDEA上,项目的文件夹有一些改变,在之前的小项目中喜欢把lib文件夹放在项目根 ...
- 笔记:Spring Cloud Hystrix 封装命令
使用继承的方式来创建Hystrix 命令,不是用注解的方式来使用 Hystrix 创建 HelloGetCommand 对象,继承与 HystrixCommand 并实现 run 方法,示例如下: p ...
- Iview的开发之路
采用了Vue-cli的方式. 1.反向代理 devServer: { host: '127.0.0.1', port: 9000, proxy: { '/gonghui/': { target: 'h ...
- 二分查找(binary search)java实现及时间复杂度
概述 在一个已排序的数组seq中,使用二分查找v,假如这个数组的范围是[low...high],我们要的v就在这个范围里.查找的方法是拿low到high的正中间的值,我们假设是m,来跟v相比,如果m& ...
- zabbix通过SNMP监控服务器硬件及构建触发器
公司的服务器没装系统无法使用IPMI协议来监控服务器硬件信息,所以我们使用SNMP来获取,下面介绍如何通过SNMP监控服务器硬件信息. 1.HP服务器进入iLO开启SNMP协议. 2.查看服务器温度信 ...
- Java注解(1)-注解基础
注解(Annotation)是在JAVA5中开始引入的,它为在代码中添加信息提供了一种新的方式.注解在一定程度上把元数据与源代码文件结合在一起,正如许多成熟的框架(Spring)所做的那样.那么,注解 ...
- Java基础学习笔记十三 常用API之正则表达式、Date、DateFormat、Calendar
正则表达式 正则表达式(英语:Regular Expression,在代码中常简写为regex).正则表达式是一个字符串,使用单个字符串来描述.用来定义匹配规则,匹配一系列符合某个句法规则的字符串.在 ...
- alpha冲刺第二天
一.合照 二.项目燃尽图 三.项目进展 图形界面基本完成 接口文档框架完成,接下来将会不断细化填充 登录界面向服务器请求数据进行ing 四.明日规划 1.注册登录接口能够完成 2.研究idea实现获得 ...
- 阿尔法冲刺——Postmortem会议
设想与目标 1.我们软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 这个问题,我们觉得我们的软件目标还是比较明确的,在SRS中也给出了典型用户和典型场景的清晰的描述. 2 ...