堆应用The kth great number
来源:互联网 发布:ug侧铣头编程 编辑:程序博客网 时间:2024/06/06 15:43
题目介绍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.
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的数字是哪一个,输入I 的熟识输入数字,输入Q的时候输出答案,这个题分析显然是对堆的性质的最好应用,创建一个堆,因为堆为单调的,所以堆顶的元素必为最大或者最小,这个题要求求第K的元素,显然就可以维护一个大小为K的堆,从小到大排列堆顶元素就是第K小的元素,对于这个堆的维护,是这样的,当输入的这个数比堆顶元素还小的时候,显然就没有入堆的必要了,当这个元素比堆顶元素大,然后就将这个元素入堆,弹出堆顶元素,这样就可以使堆的元素各入一直保持为K。
AC代码
#include<cstdio>#include<queue>#include<vector>using namespace std;int main(){ int n,k,i,q,j; char s[2]; while(scanf("%d%d",&n,&k)!=EOF){priority_queue<int,vector<int>,greater<int>> qi;for(i=1;i<=n;i++){scanf("%s",&s);if(s[0]=='I'){scanf("%d",&q);qi.push(q);if(qi.size()>k){qi.pop();}}if(s[0]=='Q'){printf("%d\n",qi.top());}}}return 0; }
- 堆应用The kth great number
- [堆] HDU4006 The kth great number
- The kth great number
- The kth great number
- The kth great number
- The kth great number
- The kth great number
- The kth great number
- The kth great number
- The kth great number
- HDU 4006 The kth great number(优先队列、堆实现)
- HDU 4006 The kth great number (堆实现优先队列)
- HDU4006 The kth great number
- HDU4006--The kth great number
- HDU4006(The kth great number)
- 【HDU4006】The kth great number
- hdu4006: The kth great number
- I - The kth great number
- 从头学习linux C 冒泡法排序
- mysql右连接,判断大小
- cassandra关于集合类型的操作
- Jsp和Servlet的小记
- MOOC清华《程序设计基础》第5章第5题:用递归算法实现数组求和
- 堆应用The kth great number
- MarkDown首行缩进和换行
- 暑假集训test6
- jiva初级,基本数据类型,运算符
- WebClient的get和post动态请求
- Linux 软件包介绍------->RPM 包
- oracle日期转数字
- 0基础学Mysql:mysql入门视频教程!
- Android 倒计时