2018学校暑期集训第一天——C++与STL

例三  ——  UVA - 12100

Printer Queue

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 ≤ m ≤ n−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

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <algorithm>
using namespace std;int main(void)
{int n, m, t, aim;priority_queue<int> pq;queue<int> q;scanf("%d", &t);while (t--) {scanf("%d%d", &n, &m);aim = m;int x;for (int i = 0; i < n; i++) {scanf("%d", &x);pq.push(x);q.push(x);}int ans = 0;while (true) {int front = q.front(), top = pq.top();q.pop();if (front < top) {q.push(front);if (aim == 0)aim = (int)q.size() - 1;elseaim--;} else {ans++;pq.pop();if (aim == 0) {printf("%d\n", ans);break;} else {aim--;}}}while (!pq.empty()) {pq.pop();}while (!q.empty()) {q.pop();}}return 0;
}

注:此题主要考察优先队列

暑期集训1:C++STL 例3:UVA-12100相关推荐

  1. 暑期集训1:C++STL 例1:UVA-10815

    2018学校暑期集训第一天--C++与STL 例一  --  UVA - 10815 Andy's First Dictionary Description XY学长刚刚立下了再不过CET就直播xx的 ...

  2. 暑期集训1:C++STL 例2:UVA-10935

    2018学校暑期集训第一天--C++与STL 例二  --  UVA - 10935 Throwing cards away I Given is an ordered deck of n cards ...

  3. 暑期集训1:C++STL 练习题E:POJ-2431

    2018学校暑期集训第一天--C++与STL 练习题E --  POJ - 2431 E - 二律背反的对偶 A group of cows grabbed a truck and ventured ...

  4. 暑期集训1:C++STL 练习题D:HDU-1509

    2018学校暑期集训第一天--C++与STL 练习题D --  HDU - 1509 D - 振电迁移的日蚀 Message queue is the basic fundamental of win ...

  5. 暑期集训1:C++STL 练习题C:HDU-1263

    2018学校暑期集训第一天--C++与STL 练习题C --   HDU - 1263 C - 亡失流转的孤独 夏天来了~~好开心啊,呵呵,好多好多水果~~  Joe经营着一个不大的水果店.他认为生存 ...

  6. 暑期集训1:C++STL 练习题B:HDU-1004

    2018学校暑期集训第一天--C++与STL 练习题B --  HDU - 1004 B - 双对福音的协议 Contest time again! How excited it is to see ...

  7. 暑期集训1:C++STL 练习题A:POJ-1833

    2018学校暑期集训第一天--C++与STL 练习题A -- POJ-1833 A - 闭时曲线的引言 Time Limit: 1000MS   Memory Limit: 30000K Total ...

  8. 暑期集训4:栈,树,优先队列 例 :  UVA - 514 ​​​​​​​​​​​​​​

    2018学校暑期集训第四天--栈,树,优先队列 例题  --   UVA - 514 Rails There is a famous railway station in PopPush City. ...

  9. 暑期集训2:ACM基础算法 例2:POJ-2456

    2018学校暑期集训第二天--ACM基础算法 例二  --   POJ - 2456 Aggressive cows Farmer John has built a new long barn, wi ...

最新文章

  1. 【进展】温度监测报警器(系统)作品项目快发布了!
  2. (译)Objective-C的动态特性
  3. linuxg下c++ 类导出_为什么PDF导出功能在SOLIDWORKS Electrical中不起作用?
  4. Google Maps API 进级: GoogleMaps常用事件及应用思路1
  5. python-词云wordcloud-0223
  6. 《设计模式详解》行为型模式 - 中介者模式
  7. C++STL算法速查
  8. 笔记本电脑截屏_笔记本电脑截屏快捷键是哪个?
  9. cfile清空文件内容_DBA日常任务清单--定期清理Oracle审计文件
  10. 司空见惯 - 会议室名称
  11. 黑帽python第二版(Black Hat Python 2nd Edition)读书笔记 之 第七章 GitHub命令与控制(2)构建基于GitHub的特洛伊木马框架
  12. Android函数插桩
  13. MPS2326 Constant-On-Time (COT) Step-Down Converter DC-DC
  14. 51单片机(一)最小系统介绍
  15. linux磁盘组修复,Linux磁盘坏道的检测及修复
  16. 十年一梦,小米的原罪得到救赎了吗?
  17. 同事间一些搞笑的事情
  18. “建房收租”网络时代人人可做
  19. Python 学生管理系统+mysql+Flask
  20. 【北醒通用上位机】TF系列测试使用说明

热门文章

  1. 互联网企业安全高级指南3.7.1 攻防驱动修改
  2. 【前端】纯前端的一个‘喜欢我吗?’
  3. 在Eclipse或MyEclipse中安装findbugs插件
  4. 转:动态链接库的全局变量问题
  5. Mysql 基于 Amoeba 的 读写分离(2)
  6. pfile和spfile的区别
  7. 一套外企的数据库设计面试题
  8. oracle停止一切进程,oracle启动/停止的几种方法以及 启动和停止过程中出错的解决办法...
  9. Android逆向--如何调试smali代码?
  10. java 并发统计_java并发编程|CountDownLatch计数器