HDOJ 4006 The kth great number(优先队列)
2012-09-05 20:47
337 查看
题目:
Total Submission(s): 3770 Accepted Submission(s): 1581
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
The output consists of one integer representing the largest number of islands that all lie on one line.
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4006
题意:
I表示输入数据,当输入Q的时候就在输入的那些数据中找第K大的数
思路:
刚开始用sort排序,因为数据量大,会超时,所以必须用最小优先队列(降序排序),先把k个数压进队列,之后的数和队头比较,如果比队头校就不进队,若比队头大,就队头出队,然后压进队列里。。。
代码:
The kth great number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 3770 Accepted Submission(s): 1581
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
The output consists of one integer representing the largest number of islands that all lie on one line.
[align=left]Sample Input[/align]
8 3 I 1 I 2 I 3 Q I 5 Q I 4 Q
[align=left]Sample Output[/align]
1 2 3 HintXiao 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).
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4006
题意:
I表示输入数据,当输入Q的时候就在输入的那些数据中找第K大的数
思路:
刚开始用sort排序,因为数据量大,会超时,所以必须用最小优先队列(降序排序),先把k个数压进队列,之后的数和队头比较,如果比队头校就不进队,若比队头大,就队头出队,然后压进队列里。。。
代码:
/* sort不行 // 数据大 会超市 队列 最小优先队列 #include<iostream> #include <algorithm> using namespace std; int used[10000+10]; bool cmp(const int a,const int b) { return a > b; } int main() { int n,k; char ch; int i=0; int a[10000+10]; int flag = 0; while(cin>>n>>k;) { memset(used, 0 ,sizeof(used)); cin>>ch; if(ch == 'I') { cin>>a[i++]; } else if(ch == 'Q') { sort(a,a+n,cmp); cout<<a[k-1-flag]<<endl; flag++; } } } */ #include<iostream> #include<queue> using namespace std; const int maxn = 1000000 + 10; struct node { int t; const bool operator < (const struct node &a) const { return t > a.t; } }a[maxn]; priority_queue<struct node > q; int main() { int temp; int n,k; char ch; int i=0; int flag = 0; while(cin>>n>>k) { while(k--) { n--; cin>>ch; if(ch == 'I') { cin>>a[++i].t; //不能用a[i++]; q.push(a[i]); // i++; } } while(n--) { cin>>ch; if(ch == 'I') { cin>>a[++i].t; temp = q.top().t; if(a[i].t > temp) { q.pop(); q.push(a[i]); } // i++; } else if(ch == 'Q') { cout<<q.top().t<<endl; } } while(!q.empty()) { q.pop(); } } }
相关文章推荐
- HDOJ 4006 The kth great number(优先队列)
- hdoj 4006 The kth great number(优先队列)
- HDOJ 4006 The kth great number(优先队列)
- (转)HDOJ 4006 The kth great number(优先队列)
- HDOJ 4006 - The kth great number 优先队列水
- hdu 4006 The kth great number(优先队列)
- HDU 4006 The kth great number(优先队列)
- HDU 4006 The kth great number(优先队列)
- hdu_4006 The kth great number 优先队列
- [HDOJ] problem_4006 【The kth great number】 优先队列
- hdu 4006 The kth great number(使用优先队列)
- Hdu 4006 The kth great number (第k大元素 优先队列的几种写法)
- HDU 4006 The kth great number (优先队列)
- hdoj 4006 The kth great number
- hdu 4006 The kth great number (优先队列)
- HDOJ 4006 The kth great number
- HDU 4006 - The kth great number(优先队列)
- 杭电4006 The kth great number(优先队列)
- HDOJ 题目4006 The kth great number(优先队列水)
- [HDOJ4006]The kth great number