-
Notifications
You must be signed in to change notification settings - Fork 0
/
11286-top-down.cpp
68 lines (55 loc) · 2.24 KB
/
11286-top-down.cpp
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
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N;
cin >> N;
vector<int> absolute_min_heap(100000 + 1);
vector<int>::size_type heap_size = 0;
while (N--) {
int x;
cin >> x;
if (x == 0) {
if (heap_size == 0) {
cout << 0 << '\n';
continue;
}
cout << absolute_min_heap[1] << '\n';
if (--heap_size == 0)
continue;
int root = absolute_min_heap[heap_size + 1];
vector<int>::size_type parent_index = 1;
while (parent_index <= heap_size / 2) {
vector<int>::size_type child_index = parent_index * 2;
if (child_index < heap_size) {
if (abs(absolute_min_heap[child_index]) > abs(absolute_min_heap[child_index + 1])
|| (abs(absolute_min_heap[child_index]) == abs(absolute_min_heap[child_index + 1])
&& absolute_min_heap[child_index] > absolute_min_heap[child_index + 1]))
++child_index;
}
if (abs(root) < abs(absolute_min_heap[child_index])
|| (abs(root) == abs(absolute_min_heap[child_index]) && root < absolute_min_heap[child_index]))
break;
absolute_min_heap[parent_index] = absolute_min_heap[child_index];
parent_index = child_index;
}
absolute_min_heap[parent_index] = root;
} else {
absolute_min_heap[++heap_size] = x;
vector<int>::size_type child_index = heap_size;
while (child_index / 2 >= 1) {
if (abs(x) < abs(absolute_min_heap[child_index / 2])
|| (abs(x) == abs(absolute_min_heap[child_index / 2]) && x < absolute_min_heap[child_index / 2])) {
absolute_min_heap[child_index] = absolute_min_heap[child_index / 2];
child_index /= 2;
} else {
break;
}
}
absolute_min_heap[child_index] = x;
}
}
return 0;
}