hdu4006(优先队列)
http://acm.hdu.edu.cn/discuss/problem/post/new.php?problemid=4006
思路:一开始我是想自己编一个最小堆来实现的,回来想了想,还是直接用优先队列吧。因为要第k大,在一个测试中k值是固定的,所以,我只要保留前k大的数,然后输出最小的那个数就可以了。
#include<iostream>
#include<queue>
using namespace std;
struct ss
{
friend bool operator<(const ss a,const ss b)
{
if(a.v>b.v)
return 1;
else
return 0;
}
int v;
};
int main()
{
char s[10];
int n,k;
while(scanf("%d%d",&n,&k)>0)
{
priority_queue<ss>q;
ss t;
while(n--)
{
scanf("%s",s);
if(s[0]=='I')
{
int a;
scanf("%d",&a);
t.v=a;
q.push(t);
if(q.size()>k)
{
q.pop();
}
}
else
{
printf("%d\n",q.top());
}
}
}
return 0;
}
朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。

浙公网安备 33010602011771号