UVa: 12100 - Printer Queue

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3252

题目描述:有一些文件需要打印机打印,每个人物有不同的优先级(1-9),打印机的运作方式为:首先从打印队列里取出一个任务J,如果队列里有比J更急的任务,则直接把任务放到打印队列的尾部,否则打印任务J。输入打印队列中各个任务的优先级以及所关注的任务在队列中的位置(对首位置为0)。输出该任务完成时刻,所有任务都需要1分钟的打印时间。例如,打印队列为{1,1,9,1,1,1},目前处于对首的任务最重完成时刻为5.

思路:用一个队列存储当前打印序列,另外再用一个优先队列存储当前打印队列,判断打印队列对首的元素是否等于优先队列对首的元素,若是,则打印,并对队列和优先队列做pop操作,如不是则把队列对首元素加入到队尾。

代码如下:

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <sstream>
#include <fstream>
#include <queue>

using namespace std;

#define FILE

int main(int argc, char* argv[])
{
	#ifdef FILE
		ifstream in("data.txt");
		ofstream out("output.txt");
		cin.rdbuf(in.rdbuf());
		cout.rdbuf(out.rdbuf());
	#endif
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		int num, index;
		queue<int> q;
		priority_queue<int> pq;
		cin>>num>>index;
		for(int j=0;j<num;j++)
		{
			int rate;
			cin>>rate;
			pq.push(rate);
			q.push(rate);
		}
		int x = 0;
		while(true)
		{
			if(q.front()==pq.top())
			{
				if(x==index)
				{
					cout<<num-q.size()+1<<endl;
					break;
				}
				else
				{
					q.pop();
					pq.pop();
					x++;
				}
			}
			else
			{
				int temp = q.front();
				q.pop();
				q.push(temp);
				if(x==index)
				{
					x=0;
					index=q.size()-1;
				}
				else
				{
					x++;
				}
			}
		}
	}
	return 0;
}
时间: 2024-10-10 02:34:26

UVa: 12100 - Printer Queue的相关文章

UVa 12100 Printer Queue (习题 5-7)

传送门:https://uva.onlinejudge.org/external/121/12100.pdf 题意:队列中待打印的任务(1 <= n <= 100)带有优先级(1-9), 打印步骤为每次从队首拿出一个, 如果队列中没有优先级比该任务高的, 打印这个任务; 若有优先级高的, 把这个任务放到队尾,  并打印优先级最高的. 每打印一次耗时1分钟, 求给定任务什么时候打印. 水题A半天    不愧是弱渣.......... 最坏的情况需要maxn*maxn的空间........ fro

UVA 12100 Printer Queue(队列和优先队列,水)

1 //#include<bits/stdc++.h> 2 #include<cstdio> 3 #include<queue> 4 #include<algorithm> 5 using namespace std; 6 typedef long long ll; 7 /** 8 题意:所有任务在队列中,若当前打印的任务优先级不是最大,则移动到队列尾部.问下标为k的任务在什么时刻被打印: 9 思路:用优先队列判断优先级是否最高.用队列模拟任务 10 (1)

12100 Printer Queue(优先队列)

12100 Printer Queue12 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. Bec

UVa 12110 Printer Queue(特殊队列)

题意  模拟打印队列   队列中有优先级大于队首的元素   队首元素就排到队尾  否则队首元素出队  输出开始在p位置的元素是第几个出队的 直接模拟这个过程就行了 #include <bits/stdc++.h> using namespace std; const int N = 205; int q[N]; int main() { int cas, n, p, cnt, front, rear, i; scanf("%d", &cas); while(cas-

Printer Queue

Description 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

uva The Dole Queue

题目如下: The Dole Queue  In a serious attempt to downsize (reduce) the dole queue, The NewNational Green Labour Rhinoceros Party has decided on the followingstrategy. Every day all dole applicants will be placed in a largecircle, facing inwards. Someone

poj 3125 Printer Queue (队列)

 Printer Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3679   Accepted: 1975 Description The only printer in the computer science students' union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs i

POJ 3125 Printer Queue 数据结构 队列

Printer Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4329   Accepted: 2269 Description The only printer in the computer science students' union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs in t

UVa12100,Printer Queue

水题,1A过的 数据才100,o(n^3)都能过,感觉用优先队列来做挺麻烦的,直接暴力就可以了,模拟的队列,没用stl #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <queue> #define maxn 100+5 using namespace std; int mid[maxn],v[maxn],q[maxn*maxn