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

题意

给你n个任务的优先度(9最大),和需要的第m个位子(0开始),求打印到第m个位子的时间

打印工作方式:首先从队列头取出1个J,如果队列里有更大的,就把J放到队列最后

         否则打印J

题解1

一开始没想到用queue如何做,问题在于怎么判断后面有比他大的

于是干脆用vector模拟一下队列

代码1

 #include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pi;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T,n,m,w;
cin>>T;
while(T--)
{
vector<pi> vec;
cin>>n>>m;
for(int i=;i<n;i++)
{
cin>>w;
vec.push_back(pi(w,i));
}
int time=;
while()
{
int pr=vec[].first,pos=vec[].second,F=;
for(int i=;i<vec.size();i++)
{
if(vec[i].first>pr)
{
F=;break;
}
}
vec.erase(vec.begin());
if(F)
{
time++;
if(pos==m)
break;
}
else
vec.push_back(pi(pr,pos));
}
cout<<time<<endl;
}
return ;
}

题解2

后来想了个queue的,开个Cnt[10]数组用来存数字就可以解决了

代码2

 #include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pi;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T,n,m,w;
cin>>T;
while(T--)
{
int Cnt[]={};
queue<pi> qu;
cin>>n>>m;
for(int i=;i<n;i++)
{
cin>>w;
Cnt[w]++;
qu.push(pi(w,i));
}
int time=;
while()
{
int pr=qu.front().first,pos=qu.front().second,F=;
qu.pop();
for(int i=;i>pr;i--)
if(Cnt[i])
F=;
if(F)
{
time++;
Cnt[pr]--;
if(pos==m)
break;
}
else
qu.push(pi(pr,pos));
}
cout<<time<<endl;
}
return ;
}

UVa 12100 Printer Queue(queue或者vector模拟队列)的更多相关文章

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

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

  2. uva 12100 Printer Queue

    The only printer in the computer science students' union is experiencing an extremely heavy workload ...

  3. UVa 12100 Printer Queue (习题 5-7)

    传送门:https://uva.onlinejudge.org/external/121/12100.pdf 题意:队列中待打印的任务(1 <= n <= 100)带有优先级(1-9), ...

  4. 12100 Printer Queue(优先队列)

    12100 Printer Queue12 The only printer in the computer science students’ union is experiencing an ex ...

  5. 【UVA - 540】Team Queue (map,队列)

    Team Queue Descriptions: Queues and Priority Queues are data structures which are known to most comp ...

  6. uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟

    挺水的模拟题,刚开始题目看错了,poj竟然过了...无奈.uva果断wa了 搞清题目意思后改了一下,过了uva. 题目要求模拟木块移动: 有n(0<n<25)快block,有5种操作: m ...

  7. UVA 12100 打印队列(STL deque)

    题意: 给定n个优先级打印队列,然后从0开始编号到n-1.出队一个元素,如果他是队列中优先级最高的,打印(耗时一分钟),否则放到队尾(不耗时).给定一个m,求位置m的文件打印的时间. 分析: 用一个p ...

  8. day22 collection 模块 (顺便对比queue也学习了一下队列)

    collection 定义命名元祖,让元祖的每个元素可以通过类似对象属性的方法用".属性"及其方便的取值. 定义可前后拿取值且可迭代的双端队列 定义有顺序的字典 定义有默认值的字典 ...

  9. [python] Queue.Queue vs. collections.deque

    https://stackoverflow.com/questions/717148/queue-queue-vs-collections-deque/717199#717199 Queue,Queu ...

随机推荐

  1. php session保存到memcache或者memcached中

    本教程叫你如何将php 的session存储在 memcached中,参考了好多网页,发现错误百出,最后自己还是测试成功了,现在将结果结果分享. 1-)系统环境 : elastix2.4 (cento ...

  2. Python3 open()函数参数

    open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=No ...

  3. Executor框架(五)Executors工厂类

    Executors 简介 Executors 是一个工厂类,其提供的是Executor.ExecutorService.ScheduledExecutorService.ThreadFactory 和 ...

  4. 《opencv学习》 之 OTSU算法实现二值化

    主要讲解OTSU算法实现图像二值化:    1.统计灰度级图像中每个像素值的个数. 2.计算第一步个数占整个图像的比例. 3.计算每个阈值[0-255]条件下,背景和前景所包含像素值总个数和总概率(就 ...

  5. js 获取上传文件的字节数及内容

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. linux计划任务crontab的使用

    参考网站:https://www.cnblogs.com/intval/p/5763929.html 编辑计划任务:    crontab -e 查看计划任务:    crontab -l 使用实例: ...

  7. Alpha版本检测报告

    1.Alpha版本测试报告 发布一篇随笔,作为项目的测试报告,内容包括: (1)测试计划 测试人员 工作安排 覃一霸 编写测试报告 张江波 执行测试.截图测试结果 测试功能 功能 描述 效果 结果 登 ...

  8. Redis 通用操作1

    01, 设置值 => set key value  01.1, 设置值并添加有效期 => set key value ex 秒数 或者 set key value px 毫秒数 01.2, ...

  9. leetcode242

    public class Solution { public bool IsAnagram(string s, string t) { Dictionary<char, int> dic ...

  10. RabbitMQ 端口号解析

    转自:https://www.cnblogs.com/liuxingke/articles/9889517.html 端口号解析 12345 - 4369 (epmd), 25672 (Erlang ...