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

题干:

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 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441721.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java循环单链表类构造函数_C++实现双向循环链表

本文实例为大家分享了C实现双向循环链表的具体代码&#xff0c;供大家参考&#xff0c;具体内容如下一、概念1.在双链表中的每个结点应有两个链接指针&#xff1a;lLink -> 指向前驱结点 (前驱指针或者左链指针)rLink->指向后继结点(后驱指针或者右链指针)2.双链表常采用…

*【CodeForces - 799C】Fountains (线段树 或 树状数组,类似二元组问题)

题干&#xff1a; Arkady plays Gardenscapes a lot. Arkady wants to build two new fountains. There are n available fountains, for each fountain its beauty and cost are known. There are two types of money in the game: coins and diamonds, so each fountain cos…

java 前后的区别_java中前后++的区别

java中前后的区别发布时间&#xff1a;2020-06-22 14:38:22来源&#xff1a;亿速云阅读&#xff1a;134作者&#xff1a;Leah这篇文章将为大家详细讲解有关java中前后的区别&#xff0c;小编觉得挺实用的&#xff0c;因此分享给大家做个参考&#xff0c;希望大家阅读完这篇文章…

【CodeForces - 735A 】Ostap and Grasshopper (水题,模拟)

题干&#xff1a; On the way to Rio de Janeiro Ostap kills time playing with a grasshopper he took with him in a special box. Ostap builds a line of length n such that some cells of this line are empty and some contain obstacles. Then, he places his grassh…

java私有成员的访问_java – 使用私有成员或公共访问器的方法

我意识到这可能无法回答,但我正在寻找是否有关于是否直接使用私有成员或类方法中的公共访问器的某种指导.例如,考虑以下代码(在Java中,但在C中看起来非常相似)&#xff1a;public class Matrix {// Private Membersprivate int[][] e;private int numRows;private int numCols;…

☆【CodeForces - 764C】Timofey and a tree (思维题,树的性质)

题干&#xff1a; Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After that they paint all the n its vertices, so that the i-th vertex gets color ci. Now its time for Timofey birthday, and his mother asked him to re…

java中实现线程互斥的关键词_简单的互斥同步方式——synchronized关键字详解

2. synchronized的原理和实现细节2.1 synchronized可以用在那些地方静态方法,锁对象为当前类的class对象,不用显式指定实例方法,锁对象为当前实例对象,不用显式指定同步代码块,锁对象为括号中指定的对象&#xff0c;必须显式指定被synchronized修饰的方法或者代码块,同一时刻只…

【51nod - 1875】 丢手绢(约瑟夫问题,可打表,用STL模拟)

题干&#xff1a; 六一儿童节到了&#xff0c;小朋友们在玩丢手绢的游戏。总共有C个小朋友&#xff0c;编号从1到C&#xff0c;他们站成一个圈&#xff0c;第i(1<i<C)个人的左边是i-1&#xff0c;第1个人的左边是C。第i(1<i<C)个人的右边是i1&#xff0c;第C个人的…

mysql server远程连接_本地远程连接 MySQL server

问题MySql Server 出于安全方面考虑默认只允许本机(localhost, 127.0.0.1)来连接访问。如果想远程访问&#xff0c;需要额外做下操作。配置修改定位文件/etc/mysql/mysql.conf.d/mysqld.cnf定位属性skip-networking #注释掉 因为它是屏蔽掉一切TCP/IP连接bind-address 127.0.0…

关闭VS警告#pragma warning(disable:4996)

代码实现&#xff1a; #pragma warning(disable:4996) 1. #pragma warning只对当前文件有效&#xff08;对于.h&#xff0c;对包含它的cpp也是有效的&#xff09;&#xff0c;而不是对整个工程的所有文件有效。当该文件编译结束&#xff0c;设置也就失去作用。 2. #pragma wa…

【CodeForces - 608C】Chain Reaction (二分 或 dp ,思维)

题干&#xff1a; 题目大意&#xff1a; 题意是在一条直线上坐落着不同位置的灯塔&#xff0c;每一个灯塔有自己的power level&#xff0c;当作是射程范围。现在从最右边的灯塔开始激发&#xff0c;如果左边的灯塔在这个灯塔的范围之内&#xff0c;那么将会被毁灭。否则会被激…

java关闭文本_如何更优雅的关闭java文本、网络等资源

通常在 java 中对文本、网络资源等操作起来是很繁杂的&#xff0c;要声明&#xff0c;读取&#xff0c;关闭三个阶段&#xff0c;还得考虑异常情况。假设我们要读取一段文本显示到控制台&#xff0c;通常会有如下的代码&#xff1a;public static void main(String[] args) {Fi…

java类匿名类实例_Java匿名类,匿名内部类实例分析

本文实例讲述了Java匿名类&#xff0c;匿名内部类。分享给大家供大家参考&#xff0c;具体如下&#xff1a;本文内容&#xff1a;内部类匿名类首发日期 &#xff1a;2018-03-25内部类&#xff1a;在一个类中定义另一个类&#xff0c;这样定义的类称为内部类。【包含内部类的类可…

【HDU - 6441】Find Integer (费马大定理 + 奇偶数列法构造勾股定理)

题干&#xff1a; people in USSS love math very much, and there is a famous math problem . give you two integers nn,aa,you are required to find 22 integers bb,cc such that ananbncnbncn. Input one line contains one integer TT;(1≤T≤1000000)(1≤T≤100000…

dart与java互调_Dart与Java不同的地方

数据类型基类是num数值型的操作运算符&#xff1a; 、 - 、* 、/ 、 ~/ 、 %/ 除法 整数余数~/ 除法 取整% 取余常用属性&#xff1a; isNaN、isEven、isOdd (是否非数字、奇偶)常用方法&#xff1a;abs()、round()、floorl()、ceil()、toInt()、toDouble()double nan 0.0 / 0…

算法学习 母函数

母函数又称生成函数。定义是给出序列:a0,a1,a2,.......ak,......,那么函数G(x)a0a1*xa2*x2......ak*xk称为序列a0,a1,a2,.......ak,......的母函数(即生成函数)。 例如&#xff1a;序列1,2,3.......n的生成函数为&#xff1a;G(x)x2x23x3........nxn。点此链接:百度百科 特别…

plsq卸载 删除注册表、_win10操作系统下oracle11g客户端/服务端的下载安装配置卸载总结...

win10操作系统下oracle11g客户端/服务端的下载安装配置卸载总结一&#xff1a;前提注意&#xff1a;现在有两种安装的方式1. oracle11g服务端(64位)oracle客户端(32位)plsql(32位)2. oracle11g服务端(32位)plsql(32位)这里我选择的是第二种 原因是 &#xff1a;首先需要明确ora…

【HDU -1568】 Fibonacci(斐波那契通项公式+取对数)

Fibonacci Problem Description 2007年到来了。经过2006年一年的修炼&#xff0c;数学神童zouyu终于把0到100000000的Fibonacci数列 (f[0]0,f[1]1;f[i] f[i-1]f[i-2](i>2))的值全部给背了下来。 接下来&#xff0c;CodeStar决定要考考他&#xff0c;于是每问他一个数字&a…

java sqlserver分页查询_实现sqlserver分页查询语句

实现sqlserver分页查询语句sqlServer&#xff1a;一次查询&#xff0c;数据库只返回一页的数据。而不是取出所有的数据。pagesize&#xff1a; 每页显示记录数cureentpage&#xff1a;当前页数select * from ( select TOP pagesize * from ( SELECT TOP pagesize*cureentpage…

【HDU - 1269】迷宫城堡 (tarjan算法模板)

题干&#xff1a; 为了训练小希的方向感&#xff0c;Gardon建立了一座大城堡&#xff0c;里面有N个房间(N<10000)和M条通道(M<100000)&#xff0c;每个通道都是单向的&#xff0c;就是说若称某通道连通了A房间和B房间&#xff0c;只说明可以通过这个通道由A房间到达B房间…