首页 > 其他 > 详细

HDUOJ----4006The kth great number(最小堆...)

时间:2014-03-16 01:08:29      阅读:576      评论:0      收藏:0      [点我收藏+]

The kth great number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 6020    Accepted Submission(s): 2436


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大的数是多少?
因而普通的排序是不行的,所以很好定义为最小堆,最大堆的求解...
但是对堆的求解也有两种,第一种是构造一个k堆,然后再输入数据,不断更新维护这个k堆,我们暂时叫他第k堆吧...
这样的话,一此题给的sample  data  为列,
bubuko.com,布布扣
 
所以  红黑树来表示的话就是下面这个了...
为了这,STL  multiset....
代码:
bubuko.com,布布扣
 1 #include<iostream>
 2 #include<set>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int n,k,i,temp;
 8     char ss[2];
 9     multiset<int> sta;
10     while(scanf("%d%d",&n,&k)!=EOF)
11     {
12         sta.clear();
13         for(i=0;i<n;i++)
14         {
15             scanf("%s",ss);
16             if(*ss==I)
17             {
18                scanf("%d",&temp);
19                if(i<k) sta.insert(temp);
20                else
21                {
22                    int head=*sta.begin();
23                    if(temp>head)
24                    {
25                      sta.erase(sta.begin());
26                      sta.insert(temp);
27                    }
28                }
29             }
30             else cout<<*(sta.begin())<<endl;
31         }
32     }
33     return 0;
34 }
bubuko.com,布布扣

方法二:

采取传统的最小堆,最大堆求解..

 

 

 
 

HDUOJ----4006The kth great number(最小堆...),布布扣,bubuko.com

HDUOJ----4006The kth great number(最小堆...)

原文:http://www.cnblogs.com/gongxijun/p/3602132.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!