用一个优先队列一个普通队列,队列里的元素出队,如果出队元素和优先队列队首元素相同就执行“打印”操作,否则就扔到队尾。
Input
One line with a positive integer : the number of test cases(at most 100). Then for each test case:
• One line with two integers n and m, where n is the number of jobs in the queue (1≤n≤100)
And m is the position of your job (0≤m≤n−1). The first position in the queue is number 0,
the second is number 1,and soon.
• One line with n integers in the range 1 to 9, giving the priorities of the jobs in the queue. The
first integer gives the priority of the first job, the second integer the priority of the second job,
and soon.
Output
For each test case,print one line with a single integer;the number of minutes until your job is completely
printed,as suming that no additional print jobs will arrive.
Sample Input
3
1 0
5
4 2
1 2 3 4
6 0
1 1 9 1 1 1
Sample Output
1
2
5
#include <iostream>
#include <string>
#include <queue>
using namespace std;
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
int num, index;
queue<int> q;
priority_queue<int> pq;
cin>>num>>index;
for(int j=0;j<num;j++)
{
int rate;
cin>>rate;
pq.push(rate);
q.push(rate);
}
int x = 0;
while(true)
{
if(q.front()==pq.top())
{
if(x==index)
{
cout<<num-q.size()+1<<endl;
break;
}
else
{
q.pop();
pq.pop();
x++;
}
}
else
{
int temp = q.front();
q.pop();
q.push(temp);
if(x==index)
{
x=0;
index=q.size()-1;
}
else
{
x++;
}
}
}
}
return 0;
}