//5_3_3: Printer Queue 打印机优先队列问题 POJ3125
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 100 + 5;
int main()
{
int i,n,m,k,a[maxn],cnt,tag,head,rear,loop,print;
scanf("%d",&loop);
while(loop --)
{
scanf("%d%d",&n,&m);
for(i = 0;i < n;i ++)
scanf("%d",&a[i]);
cnt = tag = head = 0;
rear = n;
while((rear + 1) % maxn != head) //如果队列不为空
{
if(m == head) tag = 1;
k = a[head];
head = (head + 1) % maxn;
print = 1;
for(i = head;i != rear;i = (i + 1) % maxn)
{
if(k < a[i])
{
print = 0;
a[rear] = k;
if(tag == 1)
{
m = rear;
tag = 0;
}
rear = (rear + 1) % maxn;
break;
}
}
if(print)
{
cnt ++;
if(tag == 1)
{
printf("%d\n",cnt);
break;
}
}
}
}
return 0;
}
/*测试结果:通过POJ3125检测
3
1 0
5
1
4 2
1 2 3 4
2
6 0
1 1 9 1 1 1
5
请按任意键继续. . .
*/
POJ3125 Printer Queue
最新推荐文章于 2018-05-31 22:54:26 发布