[hihoCoder] 题外话·堆

简介: 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.

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.

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using namespace std;
 5 
 6 class Heap {
 7 public:
 8     Heap() {
 9         data.clear();
10     }
11 
12     inline int parent(int idx) {
13         return (idx - 1) >> 1;
14     }
15 
16     inline int left(int idx) {
17         return (idx << 1) + 1;
18     }
19 
20     inline int right(int idx) {
21         return (idx << 1) + 2;
22     }
23 
24     void max_heapify(int idx) {
25         int largest = idx;
26         int l = left(idx), r = right(idx);
27         if (l < (int)data.size() && data[l] > data[largest]) largest = l;
28         if (r < (int)data.size() && data[r] > data[largest]) largest = r;
29         if (largest != idx) {
30             swap(data[idx], data[largest]);
31             max_heapify(largest);
32         }
33     }
34 
35     void insert(int elem) {
36         data.push_back(elem);
37         int idx = data.size() - 1;
38         while (idx >= 0 && parent(idx) >= 0 && data[parent(idx)] < data[idx]) {
39             swap(data[parent(idx)], data[idx]);
40             idx = parent(idx);
41         }
42     }
43 
44     int extract_max(void) {
45         int maximum = data[0];
46         swap(data[0], data[data.size() - 1]);
47         data.erase(data.end() - 1, data.end());
48         max_heapify(0);
49         return maximum;
50     }
51 
52 private:
53     vector<int> data;
54 };
55 
56 int main(void) {
57     int events;
58     while (scanf("%d", &events) != EOF) {
59         Heap sugars;
60         for (int i = 0; i < events; i++) {
61             char order[2];
62             scanf("%s", order);
63             if (order[0] == 'A') {
64                 int sugar;
65                 scanf("%d", &sugar);
66                 sugars.insert(sugar);
67             }
68             else printf("%d\n", sugars.extract_max());
69         }
70     }
71     return 0;
72 }
目录
打赏
0
0
0
0
8
分享
相关文章
堆内存分配策略解密
本文深入探讨了Java虚拟机中堆内存的分配策略,包括新生代(Eden区和Survivor区)与老年代的分配机制。新生代对象优先分配在Eden区,当空间不足时执行Minor GC并将存活对象移至Survivor区;老年代则用于存放长期存活或大对象,避免频繁内存拷贝。通过动态对象年龄判定优化晋升策略,并介绍Full GC触发条件。理解这些策略有助于提高程序性能和稳定性。
最小堆最大堆了解吗?一文了解堆在前端中的应用
该文章详细解释了堆数据结构(特别是最小堆)的概念与性质,并提供了使用JavaScript实现最小堆的具体代码示例,包括堆的插入、删除等操作方法。
最小堆最大堆了解吗?一文了解堆在前端中的应用
JVM中的堆
这篇文章详细介绍了JVM中的堆内存,包括堆的核心概念、内存细分、堆空间大小设置以及Java 7和8版本堆内存逻辑上的不同划分。
JVM中的堆
堆的实现以及应用
我们说堆在物理上是一个数组,逻辑上它是一个完全二叉树,我们可以通过它的下标来计算父亲和孩子之间的关系。
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等