[HDOJ4006]The kth great number
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4006
提供插入查询两个操作,问这些数中第k大的数字是几。
可以用最小堆完成这个任务,把k看作是堆的容量,堆顶就是第k大的数。这样只需要保留前k大的数输出最小那个就可以了。
1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits> 6 #include <complex> 7 #include <fstream> 8 #include <cassert> 9 #include <cstdio> 10 #include <bitset> 11 #include <vector> 12 #include <deque> 13 #include <queue> 14 #include <stack> 15 #include <ctime> 16 #include <set> 17 #include <map> 18 #include <cmath> 19 20 using namespace std; 21 22 const int maxn = 100010; 23 const int INF = 2147483647; 24 int heap[maxn]; 25 int pos; 26 27 void init() { 28 pos = 0; 29 memset(heap, 0, sizeof(heap)); 30 heap[0] = -INF; 31 } 32 33 void push(int x) { 34 int i = ++pos; 35 for(; heap[i>>1] > x; i>>=1) { 36 heap[i] = heap[i>>1]; 37 } 38 heap[i] = x; 39 } 40 41 void pop() { 42 if(pos == 0) return; 43 int child = 1; 44 int i = 1; 45 int last = heap[pos--]; 46 for(; i<<1 <= pos; i=child) { 47 child = i<<1; 48 if(child != pos && heap[child] > heap[child+1]) { 49 ++child; 50 } 51 if(last > heap[child]) { 52 heap[i] = heap[child]; 53 } 54 else { 55 break; 56 } 57 } 58 heap[i] = last; 59 } 60 61 int n, k, x; 62 char cmd[2]; 63 64 int main() { 65 // freopen("in", "r", stdin); 66 while(~scanf("%d %d", &n, &k)) { 67 init(); 68 while(n--) { 69 scanf("%s", cmd); 70 if(cmd[0] == 'I') { 71 scanf("%d", &x); 72 if(pos < k) push(x); 73 else if(heap[1] < x) { 74 pop(); 75 push(x); 76 } 77 } 78 else { 79 printf("%d\n", heap[1]); 80 } 81 } 82 } 83 return 0; 84 }
转载于:https://www.cnblogs.com/kirai/p/4928014.html
[HDOJ4006]The kth great number相关推荐
- 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: [ ...
- LeetCode hard 668. Kth Smallest Number in Multiplication Table(二分答案)
题目:https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/description/ Kth Smalle ...
- #4604. The kth maximum number(整体二分 + 树套树)
#4604. The kth maximum number 给定一个大小不超过5×1055 \times 10 ^ 55×105的矩形区域,有一些点有点权. 每次询问给定x1,y1,x2,y2,kx_ ...
- 【HDU - 4006】The kth great number (优先队列,求第k大的数)
题干: Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to wri ...
- *【HDU - 4006】 The kth great number(优先队列 or 线段树)
题干: Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to wri ...
- 蓝桥杯练习【STL训练】——The kth great number
题目描述: Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to w ...
- 【HDU No. 4006】 第k 大的数 The kth great number
[HDU No. 4006] 第k 大的数 The kth great number 杭电OJ 题目地址 [题意] 小明和小宝正在玩数字游戏.游戏有n轮,小明在每轮中都可以写一个数,或者问小宝第k 大 ...
- HDU4006(The kth great number)
两种方法做:优先队列和SBT. 先说说SBT吧.... /************************************************* 题目大意: 针对每次查询,输出第K大数;算 ...
- leetcode 668. Kth Smallest Number in Multiplication Table | 668. 乘法表中第k小的数(二分查找)
题目 https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/ 题解 看了答案 Approach #3: B ...
最新文章
- 彻底疯了,要给雷锋开博客?
- linux调用函数和case,Shell篇(3)-单双引号 ,函数及case语句用法
- Asp.net使用HttpResponse.Filter 实现简繁/繁简转换
- 关于C#对Excel导出时对整行的操作
- 兼具颜值与智慧 华为Mate 10 Pro体验图赏
- 3种实现CSS 上下居中的方法
- 物联网共享单车有什么物联技术?
- GSM模块_GSM设备和网络错误代码
- echarts 折线图 中有两条线 ,在鼠标悬停时显示数据
- Spark血缘字段解析
- 蓝桥杯 第39级台阶
- 图论总结 for noip
- jQuery实现消息列表循环垂直向上滚动
- oracle生僻字解决方案
- 英雄联盟比分直播网/APP定制开发英雄联盟数据源码
- 《卓有成效的管理者》培训感悟
- Python.随机输入两个数和运算符进行四则运算
- springboot毕设项目影评网站系统4i684(java+VUE+Mybatis+Maven+Mysql)
- 为iOS的mobileconfig文件进行签名
- Python实现电阻计算器——快速计算电阻值
热门文章
- 【深度学习】DIY 人脸识别技术的探索(一)
- python如何扩展库_python的常用扩展库以及使用方式
- 修改pom文件_SpringCloud微服务全家桶-第8篇,服务的配置文件更改
- Hp linux tar 解压,tar命令的用法(百度)(HP_UX)
- android检查usb广播,Android 检测USB 音频设备
- 浅析网站优化的站长们应该怎样安排自己的工作内容呢?
- php vs lua,解析LUA与PHP在WEB应用的性能对比
- sql isnull怎么没用_SQL语言在数据工程(Data Engineering)中的运用(一)
- .dat文件写入byte类型数组_深入浅出MATLAB数据处理之文件读写
- vim 多窗口,多tab编辑