The kth great number

Problem Description

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大的数字是谁中间呢会有一些新的数字加进来。我最初的想法,你加任你加,sort天下第一。然后就有了,加的时候,我不管,问的时候,我就排序一下,然后输出。超时,好吧,问题也不是很大,可能是我保存的数字太多了,于是这次就只保存k个,如果需要更新前面的k个数,我就sort 一下,不然我就不更新,然后超时。我就想,我可以考虑用插入排序去节省时间,那么我只要sort一次,应该不会有问题,可以我一想,插入排序后面移动时间比sort还要慢一些。感觉又不行。并且有一个很大的问题就在于,当sort排一个几乎是有序的序列的时候,时间会退化的很严重,时间复杂度会上升。所以,尝试了多次之后,选择放弃使用sort,于是用了优先队列。有小根堆去保存前k个,这样的话,取数方便的一下,时间复杂度也减下来了。

 #include<bits/stdc++.h>

 using namespace std;

 priority_queue<int, vector<int>, greater<int> >a;

 int main () {
char ch;
int n,k;
while (~scanf("%d%d",&n,&k)) {
while (!a.empty())
a.pop();
while (n--) {
int num;
getchar();
scanf("%c", &ch);
if (ch == 'I') {
scanf("%d",&num);
if (a.size() < k)
a.push(num);
else{
int mid = a.top();
if (num > mid) {
a.pop(); //队列头部数据出队
a.push(num);//在队列尾部增加num数据
}
}
}else {
num = a.top();
printf("%d\n",num);
}
}
}
return ;
}

最新文章

  1. 11月13日上午ajax返回数据类型为JSON数据的处理
  2. c语言实现牛顿迭代法
  3. Python学习路程-常用设计模式学习
  4. yii2 登录、退出、自动登录
  5. [原创]Android自定义View之IndicatorView,显示当前tab页所处位置的View
  6. 什么是co-training
  7. syslog及syslog-ng详解 日志服务器
  8. Oracle Profile 使用详解--zhuanzai
  9. leetcode Largest Rectangle in Histogram 解法二
  10. 关于imx6核心板qt系统U盘挂载
  11. CentOS6.5安装图形界面
  12. Apache Shiro入门实例
  13. MVC实现省级联动
  14. intellij idea 主题大全,看不惯idea 那2种主题的来这里了
  15. 《高性能javascript》学习总结
  16. wpf研究之道——自定义Button控件
  17. jQuery获取或设置元素的宽度和高度
  18. Ubuntu 14.04 LTS 安装 NVIDIA 显卡驱动后的屏幕亮度调节问题
  19. Thread类中的join方法
  20. 【java编程】加密算法-对称加密及AES加密算法

热门文章

  1. 2016.09.03【初中部 NOIP提高组 】模拟赛A总结
  2. vmware哪个版本好用
  3. C++ 没有合适的默认构造函数(无参数构造函数)
  4. Redis 数据安全与性能保障
  5. SDRAM学习笔记
  6. Maven手动命令行导入ojdbc6
  7. windows 安装 Mongodb 数据库及操作图形化软件 Robo 3T
  8. Windows Server 部署WEB API时内部错误
  9. phpstorm的下载激活及定制使用和设置
  10. 快速排序和二分查找(Go)