就是一个队列一直添加元素在某个时候问你第k大的元素是什么
定义一个优先队列存入k个元素如果有大的元素就让最小的出列然后再让那个入列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include<cstdio>
#include<queue>
using namespace std;
struct node{
int x;
bool friend operator <(node a,node b){
return a.x>b.x;//优先小的元素
}
}a;
int n,k;
int main(){
while(~scanf("%d %d",&n,&k)){
priority_queue<node> cnt;
char s[5];
for(int i=0;i<n;i++){
scanf("%s",s);
if(s[0]=='I'){
scanf("%d",&a.x);
cnt.push(a);
if(cnt.size()>k)//判断队列中的元素是否超过了k个
cnt.pop();
}
else{
a=cnt.top();
printf("%d\n",a.x);
}
}
}
return 0;
}