-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeap data structure
More file actions
94 lines (78 loc) · 2.51 KB
/
Heap data structure
File metadata and controls
94 lines (78 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Heap Implementation (Реализация кучи)
| | |
| :--- | :--- |
| **Input** | Standard input |
| **Output** | Standard output |
### Problem Statement
In this problem, you are required to implement a **Heap** data structure. Your program must process the following types of queries:
1. **Clear**: Make the heap empty (remove all existing elements). No output is required for this operation.
2. **Add $n$**: Add the number $n$ to the heap. No output is required for this operation.
3. **Extract Max**: Remove the maximum value from the heap and print it. If the heap is empty, print `CANNOT` instead.
### Input Format
- The first line contains a natural number $k$ ($1 \le k \le 200,000$) — the total number of queries.
- The following $k$ lines contain queries of types 1, 2, or 3, each on a separate line according to the format described above.
- All numbers provided in type 2 queries are natural numbers not exceeding $10^9$.
### Output Format
For each query of type 3, print the result on a new line.
### Examples
| Input | Output |
| :--- | :--- |
| `11` <br> `2 192168812` <br> `2 125` <br> `2 321` <br> `3` <br> `3` <br> `1` <br> `2 7` <br> `2 555` <br> `3` <br> `3` <br> `3` | `192168812` <br> `321` <br> `555` <br> `7` <br> `CANNOT` |
#include <iostream>
using namespace std;
const int N = 200000;
int heap[N];
int heap_size = 0;
void siftUp(int v) {
while (v > 0 && heap[v] > heap[(v - 1) / 2]) {
swap(heap[v], heap[(v - 1) / 2]);
v = (v - 1) / 2;
}
}
void siftDown(int v) {
while (2 * v + 1 < heap_size) {
int left = 2 * v + 1;
int right = 2 * v + 2;
int maxCh = left;
if (right < heap_size && heap[right] > heap[left])
maxCh = right;
if (heap[v] >= heap[maxCh])
return;
swap(heap[v], heap[maxCh]);
v = maxCh;
}
}
void insert(int x) {
heap[heap_size++] = x;
siftUp(heap_size - 1);
}
int extractMax() {
int t = heap[0];
heap[0] = heap[--heap_size];
siftDown(0);
return t;
}
int main() {
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int cmd;
cin >> cmd;
if (cmd == 1) {
heap_size = 0;
}
else if (cmd == 2) {
int num;
cin >> num;
insert(num);
}
else if (cmd == 3) {
if (heap_size == 0) {
cout << "CANNOT" << endl;
} else {
cout << extractMax() << endl;
}
}
}
return 0;
}