用优先队列(小的在队头),只保存最大的k个数。队头是k个数中最小的,就是第k大。
#include<stdio.h>
#include<queue>
using namespace std;
struct node{
int val;
bool operator <(node T)const{
return val>T.val;
}
}t;
int main(){
int n,k,val;
char op[2];
while(~scanf("%d%d",&n,&k)){
priority_queue<node> q;//优先队列(队头>其他数)
for(int i=0;i<n;i++){
scanf("%s",&op);
if(op[0]=='I'){
scanf("%d",&val);
t.val=val;
q.push(t);
if(q.size()>k)
q.pop();
}
else
printf("%d\n",q.top());//不写等于调用第1个变量(想想指针)
}
}
}