【优先队列】HDU4006The kth great number
来源:互联网 发布:linux ssh暴力破解 编辑:程序博客网 时间:2024/06/05 02:49
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4006
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 3I 1I 2I 3QI 5QI 4Q
题目意思:
给出一个n和一个k,n为有多少条命令,命令分为两种,输入‘I’时代表增加一个数,输入‘Q’代表询问并输出第k大的数
这个题目可以用很多种方法,线段树,树状数组,小根堆,and so on......
代码:
#include<iostream>#include<algorithm>#include<queue>using namespace std;int b[1000005];int main(){ int n,k,a; char s; cin.sync_with_stdio(false); while(cin>>n>>k){ priority_queue<int,vector<int>,greater<int> >q; // 从大到小排序; while(n--){ cin>>s; if(s=='I'){ cin>>a; if(q.size()<k) q.push(a); else if(q.top()<a){ q.pop(); q.push(a); } }else cout<<q.top()<<endl; } } /* 直接暴力TLE while(cin>>n>>k){ int cnt=0; while(n--){ cin>>s; if(s=='I'){ cin>>a; if(cnt<k) b[cnt++]=a,sort(b,b+cnt); else if(b[0]<a){ b[0]=a; sort(b,b+k); } }else cout<<b[0]<<endl; } } */ return 0;}
0 0
- 【优先队列】HDU4006The kth great number
- HDU4006-The kth great number(优先队列)
- hdu_4006 The kth great number 优先队列
- The kth great number(优先队列)
- [优先队列] HDU4006 The kth great number
- HDU4006 The kth great number 优先队列
- hdoj4006The kth great number[【优先队列】
- The kth great number 优先队列
- 优先队列 The kth great number
- hdu4006 The kth great number 优先队列
- HDU4006 The kth great number(优先队列)
- The kth great number(优先队列)
- The kth great number(优先队列)
- The kth great number (优先队列)
- hdu4006The kth great number【线段树第k大】
- hdu 4006 The kth great number(使用优先队列)
- HDU 4006 The kth great number(优先队列、堆实现)
- HDOJ 4006 The kth great number(优先队列)
- POJ 2923 Relocation (状压DP+背包)
- 设计模式学习笔记
- lintcode:Binary Tree Level Order Traversal
- 新版XCode7.2.1的Instruments测试内存泄露
- 如何实现在Oracle中应用存储过程调用MatLab函数(3)
- 【优先队列】HDU4006The kth great number
- loadrunner简单使用——HTTP,WebService,Socket压力测试脚本编写
- 无法激活服务,因为它不支持 ASP.NET 兼容性
- Android开发随笔之自定义TitleBar
- Linux下iptables开放端口端示例
- 你只是追逐时髦的码农
- [BZOJ2705] [SDOI2012] Longge的问题 - 欧拉函数
- zjnu 1762 U(想法、线段树)
- 【POJ 1191】 棋盘分割(DP)