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. Kth Smallest Element in a Sorted Matrix(大顶堆、小顶堆)

    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 - [小顶堆][优先队列]

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

  4. POJ 1456 - Supermarket - [贪心+小顶堆]

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

  5. heap c++ 操作 大顶堆、小顶堆

    在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 - [小顶堆][CTSC2007]

    题目链接: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. Jquery更改table中的内容(一)

    css部分: .tab{ border:solid 1px #00aaee; text-align: left; margin:20px;}.tab tr{ border-top: solid 1px ...

  2. Tomcat 配置详解/优化方案

     转自:http://blog.csdn.net/cicada688/article/details/14451541 Service.xml Server.xml配置文件用于对整个容器进行相关的配置 ...

  3. 关于多个block问题

    在某个添加文本的页面中,leftbarbutton是删除(直接将数组中的这个string删除),rightbarbutton是完成,分别对应两个block,完成的block是一开始写的,写到了view ...

  4. Git从码云Clone代码到本地

    Git从码云或者Github 克隆代码到本地 1.下载安装Git,傻瓜式下一步下一步即可... 2.配置Git: 2.1.选择你要clone到本地的路径:右键--->$ Git Bash Her ...

  5. [译]理解Javascript的异步等待

    原文链接: https://ponyfoo.com/articles/understanding-javascript-async-await 作者: Nicolás Bevacqua 目前async ...

  6. oracle索引监控

    目的:监控oracle索引的有效性,看索引有没有被使用.然后根据监控结果删除或者调整索引. 步骤: 1.监控指定索引 命令: alter index  索引名 monitoring usage;  如 ...

  7. Squirrel: 通用SQL、NoSQL客户端

    安装 配置数据库 配置驱动 配置连接 如果你的工作中,需要使用到多个数据库,又不想在多种客户端之间切换来切换去.那么就需要找一款支持多数据库的客户端工具了.如果你要连接多个关系型数据库,你就可以使用N ...

  8. Oracle hint

    1.use_concat 网上说法: CONCATENATION和UNION/UNION ALL操作比较类似,根据OR查询条件,将一个查询分解为两个或更多的部分,然后在去掉两个部分重复的记录.由于CO ...

  9. 分布式一致性算法--Raft

    前面一篇文章讲了Paxos协议,这篇文章讲它的姊妹篇Raft协议,相对于Paxos协议,Raft协议更为简单,也更容易工程实现.有关Raft协议和工程实现可以参考这个链接https://raft.gi ...

  10. SQLite使用(三)&&核心API使用

    概述     SQLite提供了一系列接口供用户访问数据库,主要包括连接数据库,处理SQL,迭代查询结果等.本文会针对我们使用SQLite的主要场景,列出核心的API,详细介绍API的用法并给出代码用 ...