The kth great number
The kth great number
Problem Description
Input
Output
Sample Input
8 3
I 1
I 2
I 3
Q
I 5
Q
I 4
Q
Sample Output
1
2
3
Hint
分析:
就是说,给你一些数字,然后问题第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的更多相关文章
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- [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 ...
- hdu 4006 The kth great number (优先队列)
/********************************************************** 题目: The kth great number(HDU 4006) 链接: h ...
- 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: [ ...
- hdoj 4006 The kth great number【优先队列】
The kth great number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Oth ...
- The kth great number(set)
The kth great number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Oth ...
- The kth great number(优先队列)
The kth great number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Oth ...
- HDOJ4006 The kth great number 【串的更改和维护】
The kth great number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Oth ...
- 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 ...
- [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 ...
随机推荐
- 【leetcode】1186. Maximum Subarray Sum with One Deletion
题目如下: Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elemen ...
- 24.二叉树中和为某一值的路径(python)
题目描述 输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.(注意: 在返回值的list中,数组长度大 ...
- 21.栈的压入、弹出序列(python)
题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压 ...
- curl POST如何查看响应的Header(转)
curl -I 这样其实发送是HEAD请求. 下面这样发送POST请求(-X POST),同时指定Basic认证用户名密码(-u ‘andy:andy’),同时指定数据类型(-H ‘Content-T ...
- kibana花式查询
在kibana提供的界面上进行操作. POST /school/student/_bulk{ "index": { "_id": 1 }}{ "nam ...
- Java中创建String的两种方式
1.在Java中,创建一个字符串有两种方式 String x = "abc";String y = new String("abc"); 这两种方式有什么区别呢 ...
- "error" : "Content-Type header [application/x-www-form-urlencoded] is not supported"
https://blog.csdn.net/weixin_40161254/article/details/86000839 Es Head https://www.cnblogs.com/afeig ...
- 阶段2 JavaWeb+黑马旅游网_15-Maven基础_第5节 使用骨架创建maven的java工程_18maven的java工程取mysql数据库
使用maven创建ava功能,然后读取数据库做一个测试. 我们做的持久层,没有和页面有交互,只做一个java工程就可以了 创建的是java工程,用不用骨架都可以.这里不使用骨架,直接next 直接fi ...
- Ajax请求Json数据,报500错误,后台没有错误日志。
post请求:http://localhost:9080/DataDiscoveryWeb/issueformcount/queryIssueTendencyDetail.xhtml?jobId=86 ...
- Apache配置文件介绍
一.配置文件存放位置 apache配置文件名为httpd.conf 1.yum安装 yum安装后,apache配置文件httpd.conf存放在目录/etc/httpd/conf下 2.源码编译安装 ...