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

时间:2021-07-07 10:05:56
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大...
也可以手写一个小定堆,之前知道用优先队列却不理解为什么要小顶堆,好弱啊,优先队列不就是小顶堆嘛=_=,原理一样。。。
 #include <cstring>
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int Max = ;
int Heap[Max];
int cnt;
void up(int root) // 上调
{
while (root != ) // 调到根时就结束
{
int father = root / ;
if (Heap[father] > Heap[root])
{
swap(Heap[father], Heap[root]);
root = father;
}
else
break;
}
}
void heap_push(int num)
{
Heap[++cnt] = num;
up(cnt);
}
void down(int root)
{
while (root < cnt)
{
if (root * > cnt)
break;
int son = root * ;
if (root * + <= cnt) // 找到左右子节点中较小的那个
{
if (Heap[root * + ] < Heap[son])
son = root * + ;
}
if (Heap[root] > Heap[son])
{
swap(Heap[root], Heap[son]);
root = son;
}
else
break;
}
}
void heap_pop()
{
Heap[] = Heap[cnt--];
down();
}
int main()
{
int n, k;
while (scanf("%d%d", &n, &k) != EOF)
{
char str;
getchar();
cnt = ;
int num;
for (int i = ; i < n; i++)
{
scanf("%c", &str);
getchar();
if (str == 'I')
{
scanf("%d", &num);
getchar();
heap_push(num);
if (cnt > k)
heap_pop();
}
else
printf("%d\n", Heap[]);
}
}
return ;
}

HDU 4006The kth great number(K大数 +小顶堆)的更多相关文章

  1. 378&period; Kth Smallest Element in a Sorted Matrix&lpar;大顶堆、小顶堆&rpar;

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  2. 大顶堆与小顶堆应用---寻找前k小数

    vector<int> getLeastNumber(vector<int>& arr,int k){ vector<int> vec(k,); if(== ...

  3. POJ 2442 - Sequence - &lbrack;小顶堆&rsqb;&lbrack;优先队列&rsqb;

    题目链接:http://poj.org/problem?id=2442 Time Limit: 6000MS Memory Limit: 65536K Description Given m sequ ...

  4. POJ 1456 - Supermarket - &lbrack;贪心&plus;小顶堆&rsqb;

    题目链接:http://poj.org/problem?id=1456 Time Limit: 2000MS Memory Limit: 65536K Description A supermarke ...

  5. heap c&plus;&plus; 操作 大顶堆、小顶堆

    在C++中,虽然堆不像 vector, set 之类的有已经实现的数据结构,但是在 algorithm.h 中实现了一些相关的模板函数.下面是一些示例应用 http://www.cplusplus.c ...

  6. python 基于小顶堆实现随机抽样

    起因:之前用蓄水池抽样,算法精简,但直观性很差. 所以这次采用了简单的,为没一个行,赋值一个随机值,然后取 最大的K个作为,随机样本. 基本思路:为每一个行(record,记录,实体) 赋一个rand ...

  7. Python使用heapq实现小顶堆(TopK大)、大顶堆(BtmK小)

    Python使用heapq实现小顶堆(TopK大).大顶堆(BtmK小) | 四号程序员 Python使用heapq实现小顶堆(TopK大).大顶堆(BtmK小) 4 Replies 需1求:给出N长 ...

  8. BZOJ 1150 - 数据备份Backup - &lbrack;小顶堆&rsqb;&lbrack;CTSC2007&rsqb;

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1150 Time Limit: 10 Sec Memory Limit: 162 M De ...

  9. 《排序算法》——堆排序(大顶堆,小顶堆,Java)

    十大算法之堆排序: 堆的定义例如以下: n个元素的序列{k0,k1,...,ki,-,k(n-1)}当且仅当满足下关系时,称之为堆. " ki<=k2i,ki<=k2i+1;或k ...

随机推荐

  1. Android高级之十二讲之如何降低应用内存消耗

    安卓应用的内存往往是有限的,从开始的8M到16M,24M,32M,48M,64M等逐步变大,但内存的变大是由于分辨率的提高导致,并不意味着可以随意声明使用内存,而不及时回收(即使Java有自己的垃圾回 ...

  2. Mysql 声明变量

    Mysql 声明变量 Mysql中声明变量有两种方式 第一种: set @num=1; 或set @num:=1; //这里要使用变量来保存数据,直接使用@num变量 第二种: select @num ...

  3. VIM的一些操作小技巧

    vim的设计理念是:组合. 命令的组合,模式的组合,     普通模式 左: h 上:k 下:j 右 : l   i : 当前光标处插入 I: 到光标所在行的行首进入插入模式 a: 在当前光标的后一位 ...

  4. 如何保持自己 fork 的项目和原始项目同步

    首先先通过 github 的 web 页面 fork 目标的项目 前提是自己已经设置好了git,并且配置了相应的权限 然后使用git clone命令在本地克隆自己 fork 的项目: git clon ...

  5. Rabbitmq -Routeing模式- python编码实现

    (using the pika 0.10.0 Python client) In the previous tutorial we built a simple logging system. We ...

  6. S1 : 传递参数

    ECMAScript 中所有函数的参数都是按值传递的.也就是说,把函数外部的值复制给函数内部的参数,就和把值从一个变量复制到另一个变量一样.基本类型值的传递如同基本类型变量的复制一样,而引用类型值的传 ...

  7. Python建立socket并获取信息

    import socket, sys port = 80 host = "www.baidu.com" print "Creating socket..." s ...

  8. javascript的运行过程以及setTimeout的运行机制

    关于javascript的运行机制大家都应该有所了解了吧,其实javascript是一个单线程的机制,但是因为队列的关系它的表现会让我们感觉是一个多线程的错觉.javascript在运行的时候是这样的 ...

  9. Python Django 2&period;2登录功能&lowbar;2

    #Now 让我们继续对上篇的登录进行操作 #对于csrf,以后再开篇章记录 #修改index.html <form method="post" action="/l ...

  10. Redis缓存雪崩、缓存穿透、热点Key解决方案和分析

    缓存穿透 缓存系统,按照KEY去查询VALUE,当KEY对应的VALUE一定不存在的时候并对KEY并发请求量很大的时候,就会对后端造成很大的压力. (查询一个必然不存在的数据.比如文章表,查询一个不存 ...