一道关于stl 队列的题目,比较简单。
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;
struct node {
int seq;
int pri;
node(int s,int p):seq(s),pri(p){}
};
queue<node> q;
int ans;
//判断队列头节点是否能够打印
bool is_print(node v){
int len = q.size();
bool flag = true;
queue<node> s = q;
while(!s.empty()){
if(s.front().pri > v.pri) return false;
else s.pop();
}
return true;
}
int main(){
int T,n,m,p;
cin >> T;
while(T--){
while(!q.empty()) q.pop();
ans = 1;
cin >> n >> m;int cnt =n;int i=0;
while(cnt--){
cin >> p;
node nd(i,p);i++;
q.push(nd);
}
while(1){
if(q.front().seq == m && is_print(q.front())) break;
else if(q.front().seq != m && is_print(q.front())) {ans++;q.pop();}
else if(!is_print(q.front())) {q.push(q.front());q.pop();}
else{}
}
cout<<ans<<endl;
}
return 0;
}
原文地址:http://blog.csdn.net/iboxty/article/details/45968059