The kth great numbe+优先队列

原题链接

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;
}
发布了39 篇原创文章 · 获赞 1 · 访问量 553

猜你喜欢

转载自blog.csdn.net/qq_45249273/article/details/104578840
今日推荐