HDU 4006 The kth great number【优先队列】

时间:2023-03-09 14:52:33
HDU 4006 The kth great number【优先队列】

题意:输入n行,k,如果一行以I开头,那么插入x,如果以Q开头,则输出第k大的数

用优先队列来做,将队列的大小维护在k这么大,然后每次取队首元素就可以了

另外这个维护队列只有k个元素的时候需要注意一下,先将输入的数都插入之后再将多余的数弹出去,这样才能保证留在队列里面的数是前k大的数

另外想到set里面的数是从小到大排列好了的,每次取第k个元素,但是超时了= =

 #include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
#define mod=1e9+7;
using namespace std; typedef long long LL;
const int INF = 0x7fffffff; int main(){ int n,m,i,j,k,ans;
char ch;
int x;
while(scanf("%d %d",&n,&k)!=EOF){ priority_queue<int,vector<int>,greater<int> >pq; while(n--){
cin>>ch;
if(ch=='I'){
cin>>x;
pq.push(x);
if(pq.size()>k) pq.pop();
} else if(ch=='Q'){
int y=pq.top();
printf("%d\n",y);
}
}
}
return ;
}