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)-Ⅰ
许多应用程序都需要处理有序的元素,但不一定要求他们全部有序,或者是不一定要以此就将他们排序.很多情况下我们会手机一些元素,处理当前键值最大的元素,然后再收集更多的元素,再处理当前键值最大的元素.如此这 ...
随机推荐
- 【django之分页器】
一.什么是分页功能 二.Django的分页器(paginator) 语法: paginator = Paginator(book_list, 8) #8条一页print("count:&qu ...
- AngularJS应用,常用数组知识点
AngularJS 1:ng-click,ng-model,ng-bind,ng-class,ng-hide,ng-app 2:placeholder, 3:{}中加入代码“:true|false”, ...
- Python类中的self到底是干啥的
Python类中的self到底是干啥的 Python编写类的时候,每个函数参数第一个参数都是self,一开始我不管它到底是干嘛的,只知道必须要写上.后来对Python渐渐熟悉了一点,再回头看self的 ...
- 小程序实现非swiper组件的自定义伪3D轮播图
效果如下: 我用了很笨的方法实现的,大致就是: 1.当前点击的div(view)如果前后都有内容,那么,当前div(view)就设置到中间,前一个就设置到左边,前一个的前面所有全部设置到最左边,后面一 ...
- Matlab绘图基础——绘制三维曲线
%% 绘制三维曲线 %plot3函数,其中每一组x,y,z组成一组曲线的坐标参数,选项的定义和plot函数相同. %1.当x,y,z是同维向量时,则x,y,z 对应元素构成一条三维曲线. x0 = 0 ...
- php 类接口继承练习
<?php /** * @hypo 接口的特性:接口中定义的所有方法都必须是public 接口的实现:一个接口可以使用implements操作符,类中必须实现接口中的所有方法,否则会报fatal ...
- 基于Multiple treatment的营销评估算法
营销是发现或挖掘准消费者和众多商家需求,通过对自身商品和服务的优化和定制,进而推广.传播和销售产品,实现最大化利益的过程.例如,银行可通过免息卡或降价对处在分期意愿边缘的用户进行营销,促使其分期进而提 ...
- Git详细教程(1)---个人Git的基本使用
分布式版本控制系统--git 一.什么是Git 1.Git是什么 Git是目前世界上最先进的分布式版本控制系统(没有之一). 实际上版本控制系统有如下几个: 1) CVS 2) SVN 3) Git ...
- Kaggle竞赛 —— 房价预测 (House Prices)
完整代码见kaggle kernel 或 Github 比赛页面:https://www.kaggle.com/c/house-prices-advanced-regression-technique ...
- 假设检验(Hypothesis Testing)
假设检验(Hypothesis Testing) 1. 什么是假设检验呢? 假设检验又称为统计假设检验,是数理统计中根据一定假设条件由样本推断总体的一种方法. 什么意思呢,举个生活中的例子:买橘子(借 ...