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. IT菜鸟的生存指南(二)新手村任务

    此文献给那些刚误入IT行业的小菜鸟们,此文无法教你如何"当上CEO,迎娶白富美",那是电视剧情.现实IT行业里危机重重,竞争激励.这里教你的是如何生存.生存.生存- 恭(不)喜(幸 ...

  2. Eos开发——构造查询条件

    1.ajax 方式 var data = { orgid :orgid,year:year ,month: month,type:type,sortField:'sellEmpname' ,sortO ...

  3. css 垂直居中

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  4. 当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式。

    当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式.比如 select * from T_Employee where FNumber not in ( select top 5*  ...

  5. VMware的三种网络连接方式区别

    关于VMware的三种网络连接方式,NAT,Bridged,Host-Only ,在刚接触的时候通常会遇到主机Ping不通虚拟机而虚拟机能Ping得通主机:主机与虚拟机互不相通等等网络问题.本文就这三 ...

  6. 开源监控软件ganglia安装手册

    Ganglia是一个监控服务器,集群的开源软件,能够用曲线图表现最近一个小时,最近一天,最近一周,最近一月,最近一年的服务器或者集群的cpu负载,内存,网络,硬盘等指标. Ganglia的强大在于:g ...

  7. 2015-12-01 SQL查询语句基础

    1.查询全体学生的学号与姓名select sno,snamefrom student;3.查询全体学生的详细信息select *from student;4.查询全体学生的姓名及其出生年份select ...

  8. 用css改变默认的checkbox样式

    自己常用的改变checkbox样式的两个方法: 一.利用background用图片代替checkbox效果 缺点:你首先得有一张好看的图片 优点:浏览器兼容性好 <!doctype html&g ...

  9. resize2fs命令使用

    如果我们创建一个loop设备然后将其挂载后,但是还要加大空间时,可以使用resize2fs命令来增大. 首先创建一个loop设备 dd 之后做成loop设备 losetup /dev/loop0 a. ...

  10. 安装Appium-windows

    安装Appium-windows JDK 安装JDK后设置 环境变量,把环境变量添加到你的系统PATH变量中. 变量: JAVA_HOME 值: C:\Program Files (x86)\Java ...