UVA-12100 Printer Queue

 思路:

利用表示优先级的倒叙数组 和 含有优先级和编号的队列解决

代码:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<string>
#include<cassert>
#include<cctype>
#include<memory.h>
#include<cstdio>
#include<sstream>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cstdlib>
using namespace std;
class txt	//文件 有优先级和编号
{
public:
	int grade;
	int number;
	txt(int a, int b) :grade(a), number(b){}
};
int arr[105];
bool compare(int a, int b)
{
	return a > b;
}
int main()
{
	int T = 0;
	cin >> T;
	while (T--)
	{
		memset(arr, 0, sizeof(arr));
		queue<txt> q;
		int n = 0, num = 0;
		cin >> n >> num;
		for (int i = 0; i < n; i++)
		{
			int grade = 0;
			cin >> grade;
			txt t(grade, i);
			arr[i] = grade;
			q.push(t);
		}
		sort(arr, arr + n, compare);	//倒叙记录优先级顺序
		int k = 0;	//arr[k]表示当前最大高优先级
		while (1)	//k+1表示时刻
		{
			txt t = q.front();
			if (t.grade < arr[k])
			{
				q.pop();
				q.push(t);
			}
			else		//该情况 只能是t.grade==arr[k]
			{    //因为不可能出现 低级文件比高级文件先打印
				if (t.number == num)
				{
					cout << k+1 << endl;
					break;
				}
				q.pop();
				k++;
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值