题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3252
题意:输入一个队列,每个元素都有优先级,如果队首是优先级最高的,则符合要求,出队;否则移到队尾。问初始队列中第i个元素经过多久才出队的。
说明:queue中似乎没有遍历元素的方法,用deque可以遍历队列,同时实现queue的功能。
#include<stdio.h>
#include<queue>
#include<iostream>
using namespace std;
struct printer{
int dat;
int num;
}a;
int main(){
int T,N,M;
cin >> T;
while(T--){
deque<printer> p;
cin >>N >>M;
int tot=0;//tot代表总数,也就是时间
for(int i=0;i<N;i++)
{
cin>> a.dat;
a.num=i;
p.push_back(a);
}
while(!p.empty()){
int maxn=-1;
for(int i=0;i<p.size();i++) //求当前队列中最高的优先级
if(maxn<p[i].dat) maxn=p[i].dat;
while(p[0].dat!=maxn){//如果当前队首优先级不是最高,移到队尾
p.push_back(p[0]);
p.pop_front();
}
tot++;
if(p[0].num==M) break;
p.pop_front();//队首出列
}
cout << tot<<endl;
}
return 0;
}