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

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

 

#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;
}

相关文章:

  • 2022-02-13
  • 2022-12-23
  • 2022-12-23
  • 2021-10-31
  • 2021-11-04
  • 2021-09-09
  • 2021-08-20
猜你喜欢
  • 2021-12-28
  • 2022-12-23
  • 2021-09-05
  • 2022-12-23
  • 2021-12-05
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案