题目大意:给出长度为n的一个队列,你所在的位置是m,0 <= m < n,每个任务有一个优先级,如果队列中没有优先级比当前队头的任务的优先级高,那么就直接打印这个任务,否则,就将这个任务添加到队列末尾
思路:直接用queue模拟即可
坑点:如果一个任务不打印的话是不消耗时间的
代码:
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
typedef pair < int , int > PII;
int cnt[20];//cnt数组用于记录当前队列中每个优先级的任务有多少个
//判断函数,判断当前这个任务是否可以打印
bool judge(int x)
{
for(int i = 9 ; i > x ; -- i)
if(cnt[i]) return false;
return true;
}
int main(void)
{
int T;
cin >> T;
while(T --)
{
queue <PII> q;
int n , m;
scanf("%d %d",&n , &m);
memset(cnt , 0 , sizeof cnt);
for(int i = 0 ; i < n ; ++ i)
{
int a;
scanf("%d",&a);
q.push({a , i});
cnt[a] ++;
}
int res = 0;
while(true)
{
int a = q.front().first;
int b = q.front().second;
if(judge(a))
{
cnt[a] --;
q.pop();
res += 1;
if(b == m) break;
}
else
{
PII a = q.front();
q.pop();
q.push(a);
}
}
printf("%d\n",res);
}
return 0;
}