思路:
利用表示优先级的倒叙数组 和 含有优先级和编号的队列解决
代码:
#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++;
}
}
}
}