博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDUOJ----4006The kth great number(最小堆...)
阅读量:7228 次
发布时间:2019-06-29

本文共 3354 字,大约阅读时间需要 11 分钟。

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  为列,
 
所以  红黑树来表示的话就是下面这个了...
为了这,STL  multiset....
代码:
1 #include
2 #include
3 using namespace std; 4 5 int main() 6 { 7 int n,k,i,temp; 8 char ss[2]; 9 multiset
sta;10 while(scanf("%d%d",&n,&k)!=EOF)11 {12 sta.clear();13 for(i=0;i
head)24 {25 sta.erase(sta.begin());26 sta.insert(temp);27 }28 }29 }30 else cout<<*(sta.begin())<

方法二:

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

 

 

1 /*最小堆hdu 4006*/ 2 /*@code Gxjun*/ 3 #include
4 #include
5 #define maxn 1000002 6 int heap[maxn],n,k; 7 void change(int *a ,int *b){ 8 *a^=*b , *b^=*a, *a^=*b; 9 }10 void updata_heap(int tol)11 {12 if(!(tol&1)) //是偶数数表示完全二叉树13 {14 if(heap[tol]
>1])15 change(&heap[tol],&heap[tol>>1]);16 tol--; 17 }18 for(int i=tol ; i>1 ;i-=2)19 {20 if(heap[i]>heap[i-1])21 {22 if(heap[i-1]
>1])23 change(&heap[i-1],&heap[i>>1]);24 }25 else26 if(heap[i]
>1])27 change(&heap[i],&heap[i>>1]);28 }29 }30 31 //数据更新32 void input_heap()33 {34 char ss[2];35 int i,temp;36 for(i=1 ; i<=k ;i++)37 scanf("%s %d",ss,&heap[i]);38 updata_heap(k);39 40 for(i=k+1 ;i<=n ;i++)41 {42 scanf("%s",ss);43 if(*ss=='I')44 {45 scanf("%d",&temp);46 if(temp>heap[1])47 {48 heap[1]=temp;49 updata_heap(k);50 }51 }52 else 53 if(*ss=='Q')54 printf("%d\n",heap[1]);55 }56 }57 int main()58 {59 /*freopen("test.out","w",stdout);*/60 while(scanf("%d%d",&n,&k)!=EOF)61 {62 /*memset(heap,0,sizeof(int)*(k+2));*/63 input_heap();64 }65 return 0;66 }
View Code

 

 

 
 

转载地址:http://bzdfm.baihongyu.com/

你可能感兴趣的文章
百度地图路线检索(3)
查看>>
linux netstat 命令详解
查看>>
对前几篇blog的环境等的补充说明
查看>>
Curl命令使用解析大全
查看>>
MySQL日期函数
查看>>
【00】Effective Java
查看>>
.NET重构—单元测试重构
查看>>
SMB简介sabma服务(一)
查看>>
ANT简明教程
查看>>
Eclipse Luna WTP 与 Tomcat 8 的整合存在一个很头疼的 Bug
查看>>
小数在计算机里面的存放
查看>>
数据结构中的各种树简单解释
查看>>
我的朗科运维第七课
查看>>
CentOS的进程管理二
查看>>
https客户端证书导入
查看>>
用 PreparedStatement 向 SqlServer 中一次性插入多条记录
查看>>
Slackware-2014-0903
查看>>
CentOS下安装JDK1.7
查看>>
LDAP DIT设计参考
查看>>
iptables详解
查看>>