sicily-1443. Printer Queue_printer queue sicily-程序员宅基地

技术标签: 模拟  sicily  队列  

1443. Printer Queue
限制条件
时间限制: 1 秒, 内存限制: 32 兆


题目描述


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.

输入格式


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.


输出格式


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.


样例输入
3
1 0
5
4 2
1 2 3 4
6 0
1 1 9 1 1 1


样例输出
1
2
5


题目的大意是要打印一堆job,每个job有其对应的优先级,当排在最前面的job的优先级不是最高的时候,不打印,把job移动到最后;排在最前面的是最高优先级的时候,打印。多个测试用例,每个用例输入包含n m,代表job个数,和你要打印的job在原始队列中的位置,接下来是一串数字,代表对应位置的job的优先级。你需要figure out指导你的job打印完成一共花了多长时间。每一次打印花费1时间,移动不耗时。

这个问题直接用队列模拟就行啦,下面是我的代码:


//1443. Printer Queue
// 2016.10.13

#include <iostream> 
#include <queue>
#include <algorithm>
using namespace std;

queue<int> q1,q2;   // q1 记录各个位置,q2 记录按优先级排好的值 
int arr[104],sarr[104];

bool cmp(const int &a,const int &b)   //比较函数 
	if(a>b)
		return 1;
	else
		return 0;
}
int main()
{
	int test;
	cin>>test;
	
	int n,m;
	for(int i=1;i<=test;i++)
	{
		cin>>n>>m;
		for(int i=0;i<n;i++)
		{
			cin>>arr[i];
			sarr[i]=arr[i];
			q1.push(i);
		}
		
		sort(sarr,sarr+n,cmp);
		for(int i=0;i<n;i++)
		{
			q2.push(sarr[i]);
		}
	
		int sum=0;
		
		while(arr[q1.front()] != q2.front() || q1.front() != m)   
		{
			
			if(arr[q1.front()] != q2.front())  //队列头 不为最高级的时候 
			{
				
				q1.push(q1.front()) ;
				q1.pop();
			}
			if(arr[q1.front()] == q2.front()&& q1.front() != m)   // 队列头为最高级,但不是我们要求的值 
			{
				
				q1.pop();
				q2.pop();
				sum++;
				
			}
			
		}
		while(!q1.empty())   // 清栈 
			q1.pop();
		while(!q2.empty())
			q2.pop();
		cout<<sum+1<<endl;
	}
	return 0;
}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wlb_sysu/article/details/53283750

智能推荐

Java——JComboBox下拉列表框与选中状态改变的监听_java中swing中使用combobox监听-程序员宅基地

文章浏览阅读3.6k次,点赞6次,收藏30次。设置下拉列表框大小以及位置,为下拉列表框添加选项,将下拉列表框加入到JFrame窗口 JComboBox JC=new JComboBox(); JC.setBounds(140,200,100,70);//设置下拉列表框大小以及位置 //为下拉列表框添加选项 JC.addItem("123"); JC.addItem("456"); JF.add(JC);//将下拉列表框加入到JFrame窗口......_java中swing中使用combobox监听

两个WAN设置静态路由汇聚。_两个路由 聚合-程序员宅基地

文章浏览阅读4.1k次。情况说明一台路由器,两条专网,一个内网。分别插入两个WAN口和一个LAN口。但是两个WAN口不能同时用。只能PING的一个WAN口。所以需要汇聚两个WAN口。用静态路由解决,WAN0 (GE0/7)固定IP:10.165.27.170/23 10.165.27.254需要访问网址:http://10.165.0.61需要访问网址:http://10.165.0.37:9008/..._两个路由 聚合

No cached version available for offline mode解决办法_no cached version of com.google.auto:auto-common:0-程序员宅基地

文章浏览阅读3.8k次。as加载项目build的时候不通过,出现No cached version available for offline mode,翻译为“没有可用于脱机模式的缓存版本”解决方案:preferences下搜索offline,进gradle,取消掉offline的选项即可。..._no cached version of com.google.auto:auto-common:0.11 available for offline

Single Variable Calculus 总结_sin螖x = 螖x-程序员宅基地

文章浏览阅读2.4k次。引言这篇文章是对 MIT Single Variable Calculus 这个课程的知识点总结。limit and continuity下面是 continuous 的定义: A function ff is continuous at x0x_0 if limx→x0f(x)=f(x0)\lim_{x\rightarrow x_0}f(x) = f(x_0)如果一个函数在 x0x_0 处是_sin螖x = 螖x

STM32最小系统_stm32最小系统示例-程序员宅基地

文章浏览阅读2.7w次,点赞5次,收藏21次。STM32简介STM32是一款高性能,低功耗,低成本的嵌入式ARM芯片,其家族产品大致划分如图STM32型号说明,以STM32F103ZET6芯片为例:STM32F103ZET6ARM Cortex-M内核32位微控制器芯片系列增强型席系列引脚数,Fash容量封装类型工作温度范围引脚数取值说明取值引脚数T引脚数位36R引脚数位64V引脚数位144I引脚数位176Flash容量取值说明取值_stm32最小系统示例

vue element-ui悬浮显示_elementui 悬浮窗-程序员宅基地

文章浏览阅读2k次。【代码】vue element-ui悬浮显示。_elementui 悬浮窗

随便推点

Duilib中为RichEdit\Edit控件添加自定义右键菜单_duilib richedit-程序员宅基地

文章浏览阅读7.9k次,点赞4次,收藏5次。前言Duilib中的RichEdit控件在使用中发现,基本上对复制、粘贴、剪切等快捷方式都是支持的,不过唯一缺点是没有右键菜单,感觉不够好,于是就想着加上右键菜单。右键菜单基本思路是,在RichEdit的消息处理函数中对鼠标的右键消息处理,发送一个自定义的Notify消息出来,主窗口中受到这个消息后弹出自己的右键菜单。实现方法第一步:把鼠标右键消息转_duilib richedit

MongoDB解决:Error parsing YAML config file: yaml-cpp: error at line 3, column 8: illegal map value-程序员宅基地

文章浏览阅读6.3k次,点赞8次,收藏7次。在启动MongoDB的过程中遇到的问题:出现:Error parsing YAML config file: yaml-cpp: error at line 3, column 8: illegal map value报错这个错误是属于文件格式错误解决方法:我是用的手动配置conf文件,我在配置的过程中只是敲了两个空格,但是经过查阅资料得知:mongodb 3.0之后配置文件采用YAML格式,这种格式非常简单,使用:表示,开头使用“空格”作为缩进。需要注意的是,“:”之后有value的话,需要_error parsing yaml config file: yaml-cpp: error at line 3, column 8: illegal

【C语言趣味编程100题】_趣味编程题-程序员宅基地

文章浏览阅读3.2k次,点赞4次,收藏30次。C语言趣味编程100题1.百钱百鸡——解不定方程组1.百钱百鸡——解不定方程组/*问题描述:1只公鸡5钱,1只母鸡3钱,3只小鸡1钱,现有100钱要买100只鸡,改怎么买?问题分析:设买公鸡cock只,母鸡Hen只,小鸡chicken只列出如下不定方程:cock+hen+chicken=1005cock+3hen+chicken/3=100解决方法:实质是解不定方程,使用穷举法 */#include <stdio.h>int main(){ int cock, hen_趣味编程题

设计测试用例(万能思路 + 六种设计用例方法)(详细 + 图解 + 实例)_测试用例设计-程序员宅基地

文章浏览阅读9.6k次,点赞29次,收藏238次。水杯:装水、喝水...注册场景:注册 + 登录想象日常使用中的注册场景有哪些功能。等价类是分块/分区的概念。将需求的输入划分若干个等价类,从等价类中选出一个测试用例,如果这个测试用例通过,则认为这整个等价类就通过。(因果图法)通过输入条件和输出动作之间的关系,设定判定表,再根据判定表编写测试用例。通过构造正交表编写测试用例。_测试用例设计

最小生成树MST详解_mst最小生成树-程序员宅基地

文章浏览阅读3.2k次,点赞9次,收藏42次。最小生成树(MST)是图的一个子集。本文将介绍两种常用的算法:Kruskal和Prim来寻找最小生成树。_mst最小生成树

听GPT 讲Rust源代码--src/tools(18)-程序员宅基地

文章浏览阅读1.7k次,点赞20次,收藏21次。它们用于定位代码中的特定位置,支持宽字符的处理,并提供行索引和字符编码相关的功能。综上所述,rust/src/tools/tier-check/src/main.rs这个文件的主要作用是实现"tier-check"工具,用于检查Rust编译器的编译层级,并提供有关Rust构建系统编译层级的信息。文件rust/src/tools/rust-analyzer/lib/line-index/src/lib.rs是Rust语言的语法分析器rust-analyzer的一个核心组件,用于处理代码的行列信息。

推荐文章

热门文章

相关标签