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;
}

 

posted @ 2013-01-12 11:58  紫忆  阅读(689)  评论(0编辑  收藏  举报