Sicily 1443. Printer Queue 队列操作

         题意是队列中最大的优先权的元素出队,如果队首不是最大权限,就用放到队尾,题目要求的是给定的位置,计算出队的时间

        用个数组保存最大的权限,再排序,每次检测队首元素是否与数组对应位置的权限相同,如果相同,出队,否则放到队尾

 

#include <iostream>

#include <queue>

#include <algorithm>

using namespace std;



struct job

{

	int priority;

	int number;

	job (int priority, int number) { this->priority = priority; this->number = number;}

	job(){}

};



bool cmp(int a, int b)

{

	return a > b;

}



int main()

{

	queue<job> q;

	int b[101];

	int cases;

	int n, m;

	int a, count, result;

	job tmp;



	cin >> cases;



	while (cases--)

	{

		count = 0;

		result = 0;

		cin >> n >> m;



		for (int i = 0; i < n; i++)

		{

			cin >> a;

			b[i] = a;

			q.push(job(a, i));

		}



		sort(b, b+n, cmp);



		while (1)

		{

			if (q.front().priority != b[count])

			{

				tmp = q.front();

				q.pop();

				q.push(tmp);

			}

			else

			{

				result++;

				count++;

				if (q.front().number == m) break;

				q.pop();

			}

		}

		cout << result << endl;

		while (!q.empty()) q.pop();

	}

	return 0;

}

你可能感兴趣的:(Queue)