题意 动态查询第K大的数
用小数在前优先队列维护K个数 每要插入一个数时 若这个数小于队首元素那么就不用插入了 否则队首元素出队 这个数入队 每次询问只用输出队首元素就行了
#include<cstdio>
#include<queue>
using namespace std;
int main()
{
int n, a, k;
char op[5];
while(~scanf("%d%d", &n, &k))
{
priority_queue<int,vector<int>,greater<int> > pq;
while(n--)
{
scanf("%s", op);
if(op[0] == 'I')
{
scanf("%d",&a);
if(pq.size()<k) pq.push(a);
else if(a > pq.top()) pq.push(a),pq.pop();
}
else printf("%d\n", pq.top());
}
}
return 0;
}
8 3 I 1 I 2 I 3 Q I 5 Q I 4 Q
1 2 3HintXiao Ming won‘t ask Xiao Bao the kth great number when the number of the written number is smaller than k. (1=<k<=n<=1000000).
HDU 4006 The kth great number(优先队列·第K大数)
原文地址:http://blog.csdn.net/acvay/article/details/45040501