The kth great number HDU 4006(第K大)

题面:

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.

 

 

Input

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.

 

 

Output

The output consists of one integer representing the largest number of islands that all lie on one line.

 

 

Sample Input

 

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

 

 

Sample Output

 

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大的数字是什么。由于永日至秦写下的数字非常多,你来帮思远应付他吧~

Input

本题有多组输入。在每个测试样例中,第一行包括两个正整数n,k(1=<k<=n<=1000000)。然后会有n行承接。如果永日至秦想写下一个数字,那一行会先输入'I',然后输入他写的数字。如果永日至亲想问第k大的数字是什么,他会在那一行输入'Q',然后你需要输出第k大的数字是什么。

 

永日至秦在至少写下k个数字前是不会进行询问的。

Output

当进行询问时 输出第k大的数 每个输出占用一行

Sample Input

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

Sample Output

1
2
3

代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;

typedef struct node
{
	int num;
	bool operator < (const node &a) const
	{
		return num>a.num;
	}
}point;


int main()
{
	int num1;
	int i=0;
	int n,k;
	char c;
	while(cin>>n>>k)
	{
		priority_queue<point>s;
		for(i=0;i<n;i++)
		{
			cin>>c;
			if(c=='I')
			{
				cin>>num1;
				point q;
				q.num=num1;
				s.push(q);
				if(s.size()>k)
					s.pop();
			}
			else
			{	
				cout<<s.top().num<<endl;
				
			}	
		}	
		
	}
	
	return 0;
}

我一开始 想到 对数组 用sort 排序,插进去一个数组,就排一遍序,但是太天真,狠狠的 超时。然后就想用 vector 来做,因为 他很像数组 ,直接找到 [k-1] 下标 就可以   ,但是 它好像是 没有排序的 。最后用 优先队列 的(结构体) 重载函数来模拟。题目要求 输出第K 大的那个数字,那么按照什么顺序来排序? 如果按照从大到小来排,那就是 如果元素数量大于 K时,就不能再插进去了,这时 ,队列中就会有K个元素,你要输出最后一个(最小的,该值就是第k大),先把前几个 pop掉;如果从小到大来排,与上面一样,如果数量大于 K ,那就pop ,这时 第k大 的 数 就是队列首元素(从小到大顺序),直接输出。所以我们选择从小到大顺序来排。(一开始想到优先队列的重载函数,想着重载函数一般去处理组合数据的(很多组数据)的,因为就一个元素,没有往这想;还有最后处理就卡住数量K(因为是有序的),解法很妙)。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值