第k大的题,此题用推排序做,故是优先队列的模板题
思路:先建立一个大小为k的最小堆,然后每一步判断剩余的每一个数是否比堆内的最小值大,如果比堆内的最小值大的话,就入堆。
#include<queue>
#include<iostream>
#include<cstdio>
using namespace std;
struct node
{
int num;
friend bool operator<(node aa,node bb)
{
return aa.num>bb.num;
}
}s;
int main ()
{
int n,k;
char ch;
int shu;
int ans;
while(~scanf("%d%d",&n,&k))
{
priority_queue <node> q;
ans=0;
while(n--)
{
getchar();
scanf("%c",&ch);
if(ch=='I')
{
scanf("%d",&shu);
if(ans<k)
{
++ans;
s.num=shu;
q.push(s);
}
else
{
if(q.top().num<shu)
{
q.pop();
s.num=shu;
q.push(s);
}
}
}
else if(ch=='Q')
{
printf("%d\n",q.top().num);
}
}
}
return 0;
}