A direct applicatin of the heap data structure. Specifically, a max heap is used. The required functions include insertion of a node to the heap and extraction of the maximum element of the heap. Each time you insert or remove an element to or from the heap, you need to maintain the max heap property.

The code is as follows.

If you want to learn more about heap, you may refer to this passage.

 #include <iostream>
#include <vector> using namespace std; class Heap {
public:
Heap() {
data.clear();
} inline int parent(int idx) {
return (idx - ) >> ;
} inline int left(int idx) {
return (idx << ) + ;
} inline int right(int idx) {
return (idx << ) + ;
} void max_heapify(int idx) {
int largest = idx;
int l = left(idx), r = right(idx);
if (l < (int)data.size() && data[l] > data[largest]) largest = l;
if (r < (int)data.size() && data[r] > data[largest]) largest = r;
if (largest != idx) {
swap(data[idx], data[largest]);
max_heapify(largest);
}
} void insert(int elem) {
data.push_back(elem);
int idx = data.size() - ;
while (idx >= && parent(idx) >= && data[parent(idx)] < data[idx]) {
swap(data[parent(idx)], data[idx]);
idx = parent(idx);
}
} int extract_max(void) {
int maximum = data[];
swap(data[], data[data.size() - ]);
data.erase(data.end() - , data.end());
max_heapify();
return maximum;
} private:
vector<int> data;
}; int main(void) {
int events;
while (scanf("%d", &events) != EOF) {
Heap sugars;
for (int i = ; i < events; i++) {
char order[];
scanf("%s", order);
if (order[] == 'A') {
int sugar;
scanf("%d", &sugar);
sugars.insert(sugar);
}
else printf("%d\n", sugars.extract_max());
}
}
return ;
}

[hihoCoder] 题外话·堆的更多相关文章

  1. hihoCoder#1105 题外话·堆

    原题地址 有没有更优雅地堆模板啊,总感觉我写的有些啰嗦 代码: #include <iostream> using namespace std; #define MAX_NODE 1000 ...

  2. 【HIHOCODER 1105】题外话·堆

    描述 小Ho有一个糖果盒子,每过一段时间小Ho都会将新买来的糖果放进去,同时他也会不断的从其中挑选出最大的糖果出来吃掉,但是寻找最大的糖果不是一件非常简单的事情,所以小Ho希望能够用计算机来他帮忙计算 ...

  3. HihoCoder1105 题外话·堆(基础二叉搜索树)

    第1行为1个整数N,表示需要处理的事件数目. 接下来的M行,每行描述一个事件,且事件类型由该行的第一个字符表示,如果为'A',表示小Ho将一粒糖果放进了盒子,且接下来为一个整数W,表示这颗糖果的重量: ...

  4. 题外话(简识UML语言)

    PS:“不积小流无以成为江河,不积跬步无以至千里”,学习也好,吃饭也罢,做任何事情都需要一步一个脚印,逐步积累过程,最后才会知识越来越丰富,吃的越饱… 在学习过程中用到了一些框图,用于绘画框图的语言数 ...

  5. 题外话:Lua脚本语言存在的意义

    纯属个人见解. 大致来说:c/c++执行效率高,游戏中一些性能敏感的复杂计算需要用c/c++来实现,防止游戏卡顿和低帧率.这些复杂计算包括战斗逻辑,复杂AI,骨骼动画蒙皮骨骼点的坐标计算等等.但c++ ...

  6. 题外话 -- windows10系统C盘空间变大 CPU莫名跑满

    场景描述: 安装windows10一段时间了,发现C盘空间越来越小 CPU有时候,莫名其妙的跑满,造成操作卡顿. 如何处理参考: windows10 C盘空间清理:https://jingyan.ba ...

  7. 题外话:我想立刻辞职,然后闭关学习编程语言,我给自己3个月时间学习C语言!这样行的通吗

    以下文章虽然是编程,但是对于IT行业都通用 文章背景,回答提问:我想立刻辞职,然后闭关学习编程语言,我给自己3个月时间学习C语言!这样行的通吗? 我的建议是这样:1. 不要辞职.首先说,你对整个开发没 ...

  8. 一个.net Cookie组件的bug引发的题外话

    在.net里,做过Http模拟发送请求的朋友们应该遇到过,有个时候无论怎么努力,都没办法让Cookie跟网页用浏览器所收集的一样,其中原因除了有些Cookie大概是ReadOnly之外,似乎另有隐情: ...

  9. 题外话:计算密集型 vs IO密集型

    我们把任务分为计算密集型和IO密集型,erlang作为IO密集型的语言,适合网关等相关的场景,而对计算达到某一量级后,可能处理效率下降的很明显. erlang不适合数值计算.erlang是解释型的,虽 ...

随机推荐

  1. js操作时间 加法 减法 计算 格式化时间

    Date.prototype.Format = function (fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d ...

  2. php_memcahed 安装

    1.下载服务端memcached软件 32bit:下载 memcached-win32-1.4.4-14.zip(直接下)里面包含6个文件,将解压后的文件夹随便放在什么位置(例如:D:\wamp_wi ...

  3. Apache 虚拟目录和默认首页的设置

    Apache虚拟目录和默认首页的设置,用apache服务器的朋友必须要懂的. 虚拟目录 1.找到"conf/httpd.conf" 文件 2.在节点:<IfModule al ...

  4. C++11时间操作

    C++11提供了chrono库,这个库可以处理和时间相关的一些事情.这个库里面主要有3个类:时间间隔Duration.时钟Clocks和时间点Time point. Duration template ...

  5. 联想 e460 查看 内存卡槽数

    公司配了个8g的内存条,想着给电脑安装下.电脑是新的,自带就是8g的. 拆开电脑的 “L” 形 区域后,发现两个内存卡槽都插满了,我还以为找错了呢.看了下卡槽内两个内存卡的信息,两个都是4g的. 没办 ...

  6. JSON资料整理(转载)

    目录 1.什么是json 2.json语法规则 3.json基础结构 4.json基础示例 5.JSON和XML比较 6. .NET操作JSON 原始方式 通用方式 内置方式 契约方式 通过序列化将. ...

  7. Python format 格式化函数 格式化字符串

  8. uboot中断功能实现

    uboot中实现irq中断(uboot version2015.04)1.实验目的:实现GPIO_2的外部中断 2.实验步骤:a.GPIO_2(GPIO1_IO02)为中断源, 首先需要设置这个pad ...

  9. spring 集成 redis -- pub/sub

    redis除了常用的当做缓存外,还可以当做简单的消息中间件,实现消息发布订阅 spring集成redis,可以使用spring-data-redis 首先引入相关maven依赖(此处我spring相关 ...

  10. Linq Mysql GroupBy语句的问题处理

    语句如下: var resumeList = db.ChannelResume.Where(model); var groupValues = resumeList.GroupBy(t => n ...