J - Printer Queue 优先队列与队列
来源poj3125
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
优先队列和queue的应用,把这些存在queue里,把它们的优先级存在优先队列里,如果当前的是最高的就打印,不是最高的就把他放后面去
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define scf(x) scanf("%d",&x)
#define scff(x,y) scanf("%d%d",&x,&y)
#define prf(x) printf("%d\n",x)
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+7;
const double eps=1e-8;
const int inf=0x3f3f3f3f;
using namespace std;
const double pi=acos(-1.0);
const int N=1e5+10;
struct node
{
int id,val;
};
priority_queue<int>q;
queue<node>v;
int main()
{
int re;
scf(re);
node t;
while(re--)
{
int ans=0;
while(!q.empty()) q.pop();
while(!v.empty()) v.pop();
int n,k;
scff(n,k);
rep(i,0,n)
{
scf(t.val);
t.id=i;
v.push(t);
q.push(t.val);
}
while(1)
{
t=v.front();
v.pop();
if(t.val==q.top())
{
ans++;
q.pop();
if(t.id==k)
break;
}else
v.push(t);
}
prf(ans);
}
return 0;
}
J - Printer Queue 优先队列与队列的更多相关文章
- (队列的应用5.3.3)POJ 3125 Printer Queue(优先队列的使用)
/* * POJ_3125.cpp * * Created on: 2013年10月31日 * Author: Administrator */ #include <iostream> # ...
- 【UVA】12100 Printer Queue(STL队列&优先队列)
题目 题目 分析 练习STL 代码 #include <bits/stdc++.h> using namespace std; int main() { int t; sc ...
- 12100 Printer Queue(优先队列)
12100 Printer Queue12 The only printer in the computer science students’ union is experiencing an ex ...
- UVa 12100 Printer Queue(queue或者vector模拟队列)
The only printer in the computer science students' union is experiencing an extremely heavy workload ...
- Printer Queue
Description The only printer in the computer science students' union is experiencing an extremely he ...
- Printer Queue UVA - 12100
The only printer in the computer science students' union is experiencing an extremely heavy workload ...
- POJ 3125 Printer Queue
题目: Description The only printer in the computer science students' union is experiencing an extremel ...
- uva 12100 Printer Queue
The only printer in the computer science students' union is experiencing an extremely heavy workload ...
- [刷题]算法竞赛入门经典(第2版) 5-7/UVa12100 - Printer Queue
题意:一堆文件但只有一个打印机,按优先级与排队顺序进行打印.也就是在一个可以插队的的队列里,问你何时可以打印到.至于这个插队啊,题目说"Of course, those annoying t ...
随机推荐
- 浏览器模式&用户代理字符串(IE)
问题问题描述今天在做项目的时候,QA部门提了一个Bug,在一个搜索列表中,搜索栏为空时刷新页面,却触发了搜索功能,并且列表显示出<未搜索到结果> 环境IE11 问题原因 QA的IE11用户 ...
- mysql [索引优化] -- in or替换为union all
一个文章库,里面有两个表:category和article.category里面有10条分类数据.article里面有 20万条.article里面有一个"article_category& ...
- MySQL中exists和in的区别及使用场景
exists和in的使用方式: 1 #对B查询涉及id,使用索引,故B表效率高,可用大表 -->外小内大 1 select * from A where exists (select * fro ...
- C# 枚举类型 enum
我个人感觉平日用到的enum应该是非常简单的,无非就是枚举和整数.字符串之间的转换.最近工作发现一些同事居然不太会用这个东东,于是就整理一下. 枚举类型是定义了一组“符号名称/值”配对.枚举类型是强类 ...
- 与临时对象的斗争(上)ZZ
C++ 是一门以效率见长的语言(虽然近来越来越多的人“不齿”谈及效率,我深以为不然,在某一次的程序编写中不对效率锱铢必较并不意味意味着我们就不应该追求更多的更好的做法).总之吧,相比起其它语言,程序员 ...
- 阿里云配置gitlab邮箱
gitlab_rails['gitlab_email_from'] = 'username@163.com' user['git_user_email'] = "username@163.c ...
- CAS 单点登录【2】自定义用户验证
基础不太熟的同学可以先去看:CAS 单点登录[1]入门 方案1:CAS默认的JDBC扩展方案: CAS自带了两种简单的通过JDBC方式验证用户的处理器. 1.QueryDatabaseAuthe ...
- Xtrabackup的安装
一.Installing Percona XtraBackup from Percona yum repository 添加源 yum install http://www.percona.com/d ...
- 使用Guava的RateLimiter完成简单的大流量限流
限流的一般思路: 1.随机丢弃一定规则的用户(迅速过滤掉90%的用户): 2.MQ削峰(比如设一个MQ可以容纳的最大消息量,达到这个量后MQ给予reject): 3.业务逻辑层使用RateLimite ...
- Mongodb系列- java客户端简单使用(CRUD)
Mongodb提供了很多的客户端: shell,python, java, node.js...等等. 以 java 为例实现简单的增删改查 pom文件: <dependencies> & ...