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<iostream>
  2. #include<set>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int n,k,i,temp;
  8. char ss[];
  9. multiset<int> sta;
  10. while(scanf("%d%d",&n,&k)!=EOF)
  11. {
  12. sta.clear();
  13. for(i=;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 ;
  34. }

方法二:

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

  1. /*最小堆hdu 4006*/
  2. /*@code Gxjun*/
  3. #include<stdio.h>
  4. #include<string.h>
  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&)) //是偶数数表示完全二叉树
  13. {
  14. if(heap[tol]<heap[tol>>])
  15. change(&heap[tol],&heap[tol>>]);
  16. tol--;
  17. }
  18. for(int i=tol ; i> ;i-=)
  19. {
  20. if(heap[i]>heap[i-])
  21. {
  22. if(heap[i-]<heap[i>>])
  23. change(&heap[i-],&heap[i>>]);
  24. }
  25. else
  26. if(heap[i]<heap[i>>])
  27. change(&heap[i],&heap[i>>]);
  28. }
  29. }
  30.  
  31. //数据更新
  32. void input_heap()
  33. {
  34. char ss[];
  35. int i,temp;
  36. for(i= ; i<=k ;i++)
  37. scanf("%s %d",ss,&heap[i]);
  38. updata_heap(k);
  39.  
  40. for(i=k+ ;i<=n ;i++)
  41. {
  42. scanf("%s",ss);
  43. if(*ss=='I')
  44. {
  45. scanf("%d",&temp);
  46. if(temp>heap[])
  47. {
  48. heap[]=temp;
  49. updata_heap(k);
  50. }
  51. }
  52. else
  53. if(*ss=='Q')
  54. printf("%d\n",heap[]);
  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 ;
  66. }
 
 

HDUOJ----4006The kth great number(最小堆...)的更多相关文章

  1. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  2. 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: [ ...

  3. Lintcode401 Kth Smallest Number in Sorted Matrix solution 题解

    [题目描述] Find the kth smallest number in at row and column sorted matrix. 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的 ...

  4. HDOJ4006 The kth great number 【串的更改和维护】

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

  5. HDU 4006 The kth great number 优先队列、平衡树模板题(SBT)

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

  6. [LeetCode] Kth Smallest Number in Multiplication Table 乘法表中的第K小的数字

    Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...

  7. Lintcode: Kth Prime Number (Original Name: Ugly Number)

    Ugly number is a number that only have factors 3, 5 and 7. Design an algorithm to find the kth numbe ...

  8. 排序矩阵中的从小到大第k个数 · Kth Smallest Number In Sorted Matrix

    [抄题]: 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的定义为:每一行递增,每一列也递增. [思维问题]: 不知道应该怎么加,因为不是一维单调的. [一句话思路]: 周围两个数给x或y挪一 ...

  9. 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...

随机推荐

  1. s:select 标签中list存放map对象的使用

    1.XXXAction.java private List<Map<String, String>> maptest = null; public List<Map< ...

  2. RecyclerView源码分析(一)--整体设计

    RecyclerView这个控件出来已经有一段时间了,如果看这篇文章的你,还没有使用过这个控件.那请先去学习怎样使用.不然看也白看.这里奉上一些关于介绍RecyclerView使用方法的优秀博客: 鸿 ...

  3. is-subsequence

    public class Solution { public boolean isSubsequence(String s, String t) { int idx = 0; for (int i=0 ...

  4. LoadTestAgentResultsLateException in VS2010

    遇到报错, 首先得先仔细读读人家给的出错信息. 而不是先怀疑是自己什么地方弄错了, 胡乱修改. 比如说, 遇到下面的报错: LoadTestAgentResultsLateException    R ...

  5. ItemsControl

    <ItemsControl Grid.Row=" ItemsSource="{Binding Content.patientInfoList}" Width=&qu ...

  6. Jmeter-Maven-Plugin高级应用:Modifying Properties

    Modifying Properties Pages 12 Home Adding additional libraries to the classpath Advanced Configurati ...

  7. Linux内核设计基础(四)之虚拟文件系统

    先来看一下写文件函数write的运行过程: ret = write(fd, buf, len); write适用于各种文件系统.它首先运行sys_write(),而正是这个sys_write()进行实 ...

  8. Silverlight 之 创建

          Silverlight 项目文件是您可以使用不同工具来创建和编辑的文本文件.例如,可以使用 Visual Studio 2010 以及 Expression Blend 来创建 Silve ...

  9. C#中相关结构的用法及用途

    C#中Dictionary的用法及用途 http://www.cnblogs.com/linzheng/archive/2010/12/13/1904709.html C#中的Dictionary字典 ...

  10. Linux下串口操作之数据拼接

    串口操作中,特别以非阻塞的方式读取和发送数据,做好进程之间的同步很重要.有时我们会发现这样一个问题,在进行read操作时,一次read不能获得一个完整的数据帧,这就好比你买了一个电脑,送货的先把显示器 ...