原题链接
http://acm.hdu.edu.cn/showproblem.php?pid=4006
思路
这个题就是维持一个长度为k的优先队列,然后求第k大,就是队列的头元素。
代码
#include<cstdio>
#include<queue>
using namespace std;
int main()
{
priority_queue<int,vector<int>,greater<int> >q;
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
char str[2];
while(n--)
{
scanf("%s",&str);
if(str[0]=='I')
{
int x;
scanf("%d",&x);
if(q.size()<k) q.push(x);
else if(x>q.top()) q.push(x),q.pop();
}
if(str[0]=='Q')
{
printf("%d\n",q.top());
}
}
while(!q.empty()) q.pop();
}
return 0;
}