1. 程式人生 > >關於“列印佇列 Printer Queue, ACM/ICPC NWERC 2006, UVa 12100)"的討論

關於“列印佇列 Printer Queue, ACM/ICPC NWERC 2006, UVa 12100)"的討論

列印佇列有兩個屬性,一個是優先順序一個是位置,可以寫一個結構體儲存這些資訊再利用queue的特性完成本題

#include<iostream>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 100;
struct Coor
{
	int priority;
	int position;
} co[maxn];
queue<Coor> line;
void main()
{
	int n;
	int a;
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		cin >> a;
		co[i].priority = a;
		co[i].position = i;
		line.push(co[i]);
	}
	int positon;
	cin >> positon;
	int max = 0;
	bool is_print = false;
	int count = 0;
	while(!is_print)
	{
		if(max == 0)
		{
			for(int i = 0; i < n; i++)
			{
				if(co[i].priority != 0)
				{
					if(max < co[i].priority) max = co[i].priority;
				}
			}
		}
		Coor co1 = line.front();
		if(co1.priority == max)
		{
			if(co1.position == positon)
			{
				count++;
				is_print = true;
			}
			else
			{
				co[co1.position].priority = 0;
				max = 0;
				line.pop();
				count++;
			}
		}
		else
		{
			line.pop();
			line.push(co1);
		}		
	}

	cout << count << endl;
}