堆应用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; } 

 


       


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 木工家装没事做怎么办 取票之后票丢了怎么办 补牙咬合低了点怎么办 留学生上美国网课上不了网怎么办 大四绩点不够2.0怎么办 ucas申请成绩下来后怎么办 在本校读研毕业东西怎么办 美国硕士gpa低于3.0怎么办 英国t4签证拒签怎么办 abc洗液过敏了怎么办 护士电子化注册忘记用户名怎么办 手机重力感应坏了怎么办 电脑所有驱动都删除了怎么办 毕业生没有签工作档案怎么办 澳洲两年工作签怎么办 上班一年没签劳动合同怎么办 公司拖欠工资怎么办没签劳动合同 brp丢了怎么办回国补 被怀疑假结婚该怎么办 中考作弊被捉了怎么办 露娜注册错了怎么办 去泰国开民宿怎么办工作签证 绩点2.7想出国怎么办 香港过境想去澳门怎么办 加拿大博士资格考试没通过怎么办 外国人没有学历怎么办工作签证 澳洲语言班挂了怎么办 英国学位翻译成文学硕士怎么办 没考上好的高中怎么办 毕业证12月发放申请英国怎么办 高二迷茫成绩差怎么办 高二期末考的差怎么办 法国留学签证办不下来怎么办 澳洲留学挂科签证续签怎么办 澳洲旅游签证被拒了怎么办 学校六级不让刷分怎么办 英国留学生怎么办申根签证 挂科太多拿不到学位证怎么办 ucl语言班没过怎么办 西澳大学工程挂科怎么办 澳洲语言班没过怎么办2018