题干:

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, thenmove 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 importantmuffin 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 simplifymatters, 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 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

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

题目大意:

一个打印序列,有优先级别顺序。从前往后扫描,若该作业优先级别已是最高,则执行此作业。若不是,放到队列末尾。求指定位置的作业mine被执行的时间。

解题报告:

用一个队列和优先队列来模拟整个过程就可以了,刚开始非要只用优先队列然后找个规律就可以了,结果发现有很多特殊情况需要考虑,最后放弃了这一种做法。

AC代码:

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
int n,vip,mine,res;
int main() {int t;scanf("%d",&t);while (t--) {queue<int> q;priority_queue<int> pq;scanf("%d%d",&n,&mine);mine++;for (int i = 0; i < n; ++i) {scanf("%d",&vip);q.push(vip);pq.push(vip);}res = mine;while (1) {int cur = q.front();q.pop();if (res == 1) {//如果打印的是当前任务 if (cur != pq.top()) {//还有优先级更高的。q.push(cur);res = pq.size();} else break;} else {res--;if (cur != pq.top()) {q.push(cur);}else pq.pop();}}printf("%d\n", n-q.size());}return 0;
}

错误代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX = 1e2 + 5 ;
int n,mine,myvip,myid;
int bk[15];
struct Node {int id,vip;Node(){}Node(int id,int vip):id(id),vip(vip){}bool operator < (const Node b) const{if(vip != b.vip) return vip < b.vip;return id > b.id;}
} node[MAX];
int main()
{int t;cin>>t;while(t--) {priority_queue<Node> pq;cin>>n>>mine;mine++;for(int i = 1; i<=n; i++) {scanf("%d",&node[i].vip);node[i].id=i;pq.push(node[i]);bk[node[i].vip]++;if(i==mine) myvip = node[i].vip,myid = bk[myvip]-1;//myid记录前面有几个 } int ans = 0,tmp = 0;for(int i = 1; i<=n; i++) {if(pq.top().vip == myvip) break;pq.pop();ans++;}printf("ans = %d\n",ans);for(int i = n; i>=1; i--) {if(node[i].vip > myvip) break;if(node[i].vip == myvip) tmp++;}printf("tmp = %d\n",tmp);
//      while(!pq.empty()) {
//          if(pq.top().id == mine) break;pq.pop();
//          tmp++;
//      }printf("%d\n",ans + myid + tmp + 1);}return 0 ;
}

【POJ - 3125 】Printer Queue(模拟,队列+优先队列,STL)相关推荐

  1. poj 3125 Printer Queue(STL注意事项)

    http://poj.org/problem?id=3125 这道题没什么突出的地方,是一道很水的题,可以用list,也可以用queue来解决.(用list解决的代码我就不写了)把它写上来,只是因为我 ...

  2. uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列

    题目很简单,给一个队列以及文件的位置,然后一个一个检查,如果第一个是优先级最高的就打印,否则放到队列后面,求所要打印的文件打印需要花费多长时间. 这里我用数组模拟队列实现,考虑到最糟糕的情况,必须把数 ...

  3. 数据结构实验之二叉树五:层序遍历(STL和模拟队列两种方法)

    Description 已知一个按先序输入的字符序列,如abd,eg,cf,(其中,表示空结点).请建立二叉树并求二叉树的层次遍历序列. Input 输入数据有多行,第一行是一个整数t (t<1 ...

  4. C语言编程模拟超市抹零结账,STL实践项目之用queue模拟超市结账环节

    前面章节介绍了 queue 容器适配器的具有用法,本节将利用 queue 模拟超市中结账环节运转的程序. 在超市营业过程中,结账队列的长度是超市运转的关键因素.它会影响超市可容纳的顾客数,因为太长的队 ...

  5. bash shell数组模拟队列queue和shell数组使用技巧

    一 shell数组操作模拟队列queue或者栈stack http://www.tech-recipes.com/rx/911/queue-and-stack-using-array/ here is ...

  6. STL - queue(队列)

    Queue简单介绍 queue是队列容器.是一种"先进先出"的容器. queue是简单地装饰deque容器而成为另外的一种容器. #include <queue> qu ...

  7. C++stack与queue模拟实现

    愿煦风和日永远卫护着可爱的你. stack与queue模拟实现 stack queue 为什么选择deque作为stack和queue的底层默认容器 在stl中,stack(栈)与queue(队列)都 ...

  8. java数据结构与算法之(Queue)队列设计与实现

    [版权申明]转载请注明出处(请尊重原创,博主保留追究权) http://blog.csdn.net/javazejian/article/details/53375004 出自[zejian的博客] ...

  9. 7-22 堆栈模拟队列 (25 分)

    设已知有两个堆栈S1和S2,请用这两个堆栈模拟出一个队列Q. 所谓用堆栈模拟队列,实际上就是通过调用堆栈的下列操作函数: int IsFull(Stack S):判断堆栈S是否已满,返回1或0: in ...

最新文章

  1. spark mysql 驱动_spark读取mysql数据库的驱动问题
  2. BZOJ 2592 随机化(伪)
  3. 计算机网络期中考察方案,计算机网络期中考试题 b卷_ans.docx
  4. 基于SIFT特征的全景图像拼接
  5. Scikit-learn 概述
  6. 一个简单的例子学习 HTML 元素property和attribute的区别
  7. java 缓存分页_基于redis做缓存分页
  8. 润乾V4导出TXT时自定义分隔符
  9. ajax 输入不为空,ajax POST响应为空
  10. C语言的EOF是什么?getchar()!=EOF返回的是什么?
  11. singer页左侧滚动的时候右侧跟随高亮显示
  12. python表达式3 2 3的值为_Python3中的表达式运算符
  13. 遗传算法中适值函数的标定与大变异算法
  14. Centos7 Kubernetes(k8s) 开发服务器(单服务器)部署 elasticsearch 搜索引擎
  15. Brettle.Web.NeatUpload.dll支持的大文件上传
  16. 在mac上用文本编辑器写java源代码
  17. python股票量化交易(10)---使用机器学习算法预测股票涨跌
  18. NoSQLBooster for MongoDB基本使用步骤
  19. 漫画:什么是 “幼态持续” ?
  20. 2018年内大892数据结构部分参考答案

热门文章

  1. [Leetcode][第546题][JAVA][移除盒子][递归][动态规划]
  2. python汉字长度_行中字符串的长度(Python)
  3. cpp [Error] reference to ‘count‘ is ambiguous(全局变量的使用模糊不清)
  4. dashboard windows 前端开发环境搭建
  5. java sql objects_第十五章-简书.sql
  6. std::set作为一个有序集合
  7. C++结构体实例和类实例的初始化
  8. 计算机启动软件,计算机软件及应用启动会-20210703001237.pptx-原创力文档
  9. 查询学生选修课程管理系统java_JAVA数据库课程设计学生选课管理系统的
  10. win7锁屏时间怎么设置_电脑锁屏时间怎么设置