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 ;
}

The kth great number的更多相关文章

  1. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  2. [LintCode] Kth Smallest Number in Sorted Matrix 有序矩阵中第K小的数字

    Find the kth smallest number in at row and column sorted matrix. Have you met this question in a rea ...

  3. hdu 4006 The kth great number (优先队列)

    /********************************************************** 题目: The kth great number(HDU 4006) 链接: h ...

  4. Lintcode: Kth Smallest Number in Sorted Matrix

    Find the kth smallest number in at row and column sorted matrix. Example Given k = 4 and a matrix: [ ...

  5. hdoj 4006 The kth great number【优先队列】

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

  6. The kth great number(set)

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

  7. The kth great number(优先队列)

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

  8. HDOJ4006 The kth great number 【串的更改和维护】

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

  9. HDU 4006 The kth great number 优先队列、平衡树模板题(SBT)

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

  10. [LeetCode] Kth Smallest Number in Multiplication Table 乘法表中的第K小的数字

    Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...

随机推荐

  1. 【长郡NOIP2014模拟10.22】搞笑的代码

    题目 在OI界存在着一位传奇选手--QQ,他总是以风格迥异的搞笑代码受世人围观 某次某道题目的输入是一个排列,他使用了以下伪代码来生成数据 while 序列长度<n do { 随机生成一个整数属 ...

  2. Kafka(华为FusionInsight )操作命令

    华为大数据kafka操作web界面创建角色.用户.用户管理角色进入服务器环境,进入客户端目录/opt/hadoopclient,导入环境变量source bigdata_env.切换用户kinit k ...

  3. 安装c#服务

    https://www.cnblogs.com/zmztya/p/9577440.html 1.以管理员身份运行cmd 2.安装windows服务 cd C:\Windows\Microsoft.NE ...

  4. vue2.0关于添加属性后视图不能更新的问题

    属性赋值和this.$set 和vue.$set方法我不行 可以用 this.$delete来进行删除后在设置都可以了

  5. linux常用的镜像(centos、kali、redhat等)官方下载地址

    常用的linux版本: Redhat:https://developers.redhat.com/topics/linux/ Centos:https://www.centos.org/downloa ...

  6. npm 安装与卸载模块

    1. 只卸载模块 由于之前安装过,在 package.json 中的记录仍然存在 npm uninstall lodash 2. --save 参数使用 卸载模块的同时删除在 package.json ...

  7. ELK+Filebeat+redis整合

    前面的博客,有具体的ELK安装配置步骤,此处在其基础上修改 修改配置文件并启动 [root@topcheer filebeat-6.2.3-linux-x86_64]# vim filebeat.ym ...

  8. 【转】Django restful framework中自动生成API文档

    转自 https://www.cnblogs.com/sui776265233/p/11350434.html 自动生成api文档(不管是函数视图还是类视图都能显示) 1.安装rest_framewo ...

  9. Springboot对Controller层方法进行统一异常处理

    Controller层方法,进行统一异常处理 提供两种不同的方案,如下: 方案1:使用 @@ControllerAdvice (或@RestControllerAdvice), @ExceptionH ...

  10. android sp文件一个键值保存多条信息

    之前碰到过这样的问题,sp文件只能够append,或者清空.其实一个键值,通过,分割,或者替代可以实现多条信息的存储.下面是一个举例: package com.ctbri.weather.utils; ...