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. Python简单雷达图绘制

    import numpy as npimport matplotlib.pyplot as pltimport matplotlibmatplotlib.rcParams['font.family'] ...

  2. 游标定位:Cursor类

    关于 Cursor Cursor 是每行的集合. 使用 moveToFirst() 定位第一行. 你必须知道每一列的名称. 你必须知道每一列的数据类型. Cursor 是一个随机的数据源. 所有的数据 ...

  3. TCP大文件上传与UDP协议

    一.UCP大文件上传(解决粘包问题) ①客户端 import socket, os, json, struct client = socket.socket() client.connect(('12 ...

  4. 学习日记21、IE下的Ajax需要注意的地方

    上面这张图片我是封装了一个easyui下拉框,红框出则是动态传入的json数据,这串代码在google下运行不会有任何问题,但是在IE下运行则会提示缺少:,这是因为IE只识别json格式的数据,所以这 ...

  5. python基础之 数据格式化

    %还是format 皇城PK Python中格式化字符串目前有两种阵营:%和format,我们应该选择哪种呢? 自从Python2.6引入了format这个格式化字符串的方法之后,我认为%还是form ...

  6. LeetCode_70.爬楼梯

    LeetCode-70 LeetCode_70.爬楼梯 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正 ...

  7. 异步任务分发模块Celery

    Celery简介 Celery是一个功能完备即插即用的任务队列.它使得我们不需要考虑复杂的问题,使用非常简单. celery适用异步处理问题,当遇到发送邮件.或者文件上传, 图像处理等等一些比较耗时的 ...

  8. Linux编译部署vsftp服务

    部署vsftp服务   Hostname OS IP Software Vsftpsvr RHEL-5.8 10.0.0.58 yum install -y vsftpd yum install –y ...

  9. 一个蒟蒻的解题过程记录——洛谷P1003 铺地毯

    这到题算是我“火线回归”后码的第一道题,病好了心情不错,发篇博客分享一下 目录: ·题目描述 ·题目分析 ·解题思路 ·代码实现 ·总结 ·题目描述: 为了准备一场特殊的颁奖典礼,组织者在会场的一片矩 ...

  10. leetcode234 回文链表 两种做法(stack(空间非O(1)),空间O(1))

    link: leetcode234 回文链表 方法1, 快慢指针,把前半部分存入栈中和后半部分比较 public boolean isPalindrome(ListNode head) { if(he ...