题意:给你n,m其中n是指打印文件的份数,m是你要打印的文件在队列中的位置,然后再给你n个数字代表每份文件的优先级,优先级越高的越先打印,如果当前队列队首文件优先级不是最高则将其放在队列最后,每份文件打印需一分钟,问打印到你的文件需多少分钟。
思路:
将原本序列的文件的优先级及初始位置存入一个普通队列中,然后再按优先级的大顶堆将队列优先级存入优先队列中,将普通队列队首与优先队列队首比较如果优先级不相等就排到队尾,如果相等则将分钟数加一并在两个队列中移除这分文件,如果即相等并且该元素初始位置与m相等则输出分钟数。
ac代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
struct node{
int val,post;
};
int main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t; cin>>t;
while(t--){
queue<node> l;
priority_queue<int ,vector<int>,less<int>> k;
int n,m; cin>>n>>m;
for(int i=0;i<n;i++){
node p; p.post=i; cin>>p.val;
l.push(p); k.push(p.val);
}
int ans=0;
while(!k.empty()){
int flag=0;
while(l.front().val!=k.top()){
node p=l.front(); l.pop(); l.push(p);
}
if(l.front().val==k.top()){
ans++; k.pop();
if(l.front().post==m) {
flag=1;
}
l.pop();
}
if(flag==1) break;
}
cout<<ans<<endl;
}
return 0;
}