zoukankan      html  css  js  c++  java
  • HDU 4006The kth great number(K大数 +小顶堆)

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

    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). 

    题意:8个操作,每次操作两种形式,I表示插入一个数,Q表示输出当前的第 3 大数
    最简单优先队列,从小到大顺序且容量为k,如果插入一个队列长度大于k时,那么队首出队,因为要求的是第 K 大的,那么之前的队首 肯定不是 第 K 大的,可能是 K + 1大...
    也可以手写一个小定堆,之前知道用优先队列却不理解为什么要小顶堆,好弱啊,优先队列不就是小顶堆嘛=_=,原理一样。。。
     1 #include <cstring>
     2 #include <algorithm>
     3 #include <cstdio>
     4 #include <iostream>
     5 using namespace std;
     6 const int Max = 10000000;
     7 int Heap[Max];
     8 int cnt;
     9 void up(int root)  // 上调
    10 {
    11     while (root != 1)  // 调到根时就结束
    12     {
    13         int father = root / 2;  
    14         if (Heap[father] > Heap[root]) 
    15         {
    16             swap(Heap[father], Heap[root]);
    17             root = father;
    18         }
    19         else
    20             break;
    21     }
    22 }
    23 void heap_push(int num)
    24 {
    25     Heap[++cnt] = num;
    26     up(cnt);
    27 }
    28 void down(int root)
    29 {
    30     while (root < cnt)
    31     {
    32         if (root * 2 > cnt)
    33             break;
    34         int son = root * 2;
    35         if (root * 2 + 1 <= cnt)  // 找到左右子节点中较小的那个
    36         {
    37             if (Heap[root * 2 + 1] < Heap[son])
    38                 son = root * 2 + 1;
    39         }
    40         if (Heap[root] > Heap[son])
    41         {
    42             swap(Heap[root], Heap[son]);
    43             root = son;
    44         }
    45         else
    46             break;
    47     }
    48 }
    49 void heap_pop()
    50 {
    51     Heap[1] = Heap[cnt--];
    52     down(1);
    53 }
    54 int main()
    55 {
    56     int n, k;
    57     while (scanf("%d%d", &n, &k) != EOF)
    58     {
    59         char str;
    60         getchar();
    61         cnt = 0;
    62         int num;
    63         for (int i = 0; i < n; i++)
    64         {
    65             scanf("%c", &str);
    66             getchar();
    67             if (str == 'I')
    68             {
    69                 scanf("%d", &num);
    70                 getchar();
    71                 heap_push(num);
    72                 if (cnt > k)
    73                     heap_pop();
    74             }
    75             else
    76                 printf("%d
    ", Heap[1]);
    77         }
    78     }
    79     return 0;
    80 }
    View Code
  • 相关阅读:
    大二下学期团队项目(app端web请求)
    大二下每周总结
    大二下学期团队项目(手机端分类查询前端)
    Windows 2008 R2 64位下安装Oracle 10.2.0.5
    Win7 32位下安装Oracle 10g
    SQL Developer安装使用教程
    Oracle使用DBCA建立数据库实例
    史上最全Oracle安装配置图文教程,含TNS配置
    Oracle9i完美安装、配置及操作
    安装MySQL启动服务时报错解决办法,错误代码1045
  • 原文地址:https://www.cnblogs.com/zhaopAC/p/5520937.html
Copyright © 2011-2022 走看看