首页 > ACM题库 > HDU-杭电 > HDU 4006-The kth great number-优先队列-[解题报告]HOJ
2015
04-14

HDU 4006-The kth great number-优先队列-[解题报告]HOJ

The kth great number

问题描述 :

Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to write down a number, or ask Xiao Bao what the kth great number is. Because the number written by Xiao Ming is too much, Xiao Bao is feeling giddy. Now, try to help Xiao Bao.

输入:

There are several test cases. For each test case, the first line of input contains two positive integer n, k. Then n lines follow. If Xiao Ming choose to write down a number, there will be an " I" followed by a number that Xiao Ming will write down. If Xiao Ming choose to ask Xiao Bao, there will be a "Q", then you need to output the kth great number.

输出:

There are several test cases. For each test case, the first line of input contains two positive integer n, k. Then n lines follow. If Xiao Ming choose to write down a number, there will be an " I" followed by a number that Xiao Ming will write down. If Xiao Ming choose to ask Xiao Bao, there will be a "Q", then you need to output the kth great number.

样例输入:

8 3
I 1
I 2
I 3
Q
I 5
Q
I 4
Q

样例输出:

1
2
3

Hint
Xiao 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).

/*
分析:
    据说有很多方法,不过如果想省事儿的话,
还是用优先队列吧,只存前k大的数就行了。

                                            2012-07-27
*/

#include"stdio.h"
#include"queue"
using namespace std;


struct node
{
	int num;
	friend bool operator<(node n1,node n2)
	{
		return n2.num<n1.num;
	}
};


int main()
{
	node cur,next;
	int n,k;
	int tot;
	char str[5];


	while(scanf("%d%d",&n,&k)!=-1)
	{
		priority_queue<node>q;
		tot=0;
		while(n--)
		{
			scanf("%s",str);
			if(str[0]=='Q')
			{
				cur=q.top();
				printf("%d\n",cur.num);
			}
			else
			{
				scanf("%d",&next.num);
				if(tot<k)	{q.push(next);tot++;}
				else
				{
					cur=q.top();
					if(next.num>cur.num)
					{
						q.pop();
						q.push(next);
					}
				}
			}
		}
	}


	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/ice_crazy/article/details/7795550


  1. Thanks for using the time to examine this, I truly feel strongly about it and enjoy finding out far more on this subject matter. If achievable, as you achieve knowledge

  2. [email protected]