UVA121000 [5-7]

本文介绍了一种使用优先队列和普通队列来模拟打印任务调度的算法。通过比较队列首元素与优先队列顶部元素,决定是否执行打印操作,否则将任务移至队尾,直到特定任务完成。

用一个优先队列一个普通队列,队列里的元素出队,如果出队元素和优先队列队首元素相同就执行“打印”操作,否则就扔到队尾。

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 soon.

   • One line with 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 soon.

 

Output
For each test case,print one line with a single integer;the number of minutes until your job is completely

printed,as suming 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

#include <iostream>
#include <string>
#include <queue>
using namespace std;
int main()
{
	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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值