-
Notifications
You must be signed in to change notification settings - Fork 0
/
QBHEAP.cpp
64 lines (63 loc) · 1.48 KB
/
QBHEAP.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
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define X first
#define Y second
#define pb push_back
#define fr(i,l,r) for(int i=l;i<=r;++i)
#define fd(i,r,l) for(int i=r;i>=l;--i)
#define ALL(v) (v).begin(), (v).end()
#define endl '\n'
string input;
int add,Heap[15001],a[15001];
int nHeap;
void Push(int x) {
int child, parent;
nHeap++;
child = nHeap;
parent = child/2;
while(parent > 0 && Heap[parent] < x) {
Heap[child] = Heap[parent];
child = parent;
parent = child/2;
}
Heap[child] = x;
}
void Pop() {
int x, child, parent;
x = Heap[nHeap];
nHeap--;
parent = 1;
while(2*parent <= nHeap) {
child = 2*parent;
if(child + 1 <= nHeap && Heap[child+1] > Heap[child]) child++;
if(Heap[child] <= x) break;
Heap[parent] = Heap[child];
parent = child;
}
Heap[parent] = x;
}
int main() {
char ch;
nHeap = 0;
while(cin >> ch) {
if (ch == '+') {
cin >> add;
if (nHeap < 15000) Push(add);
}
else {
if(nHeap > 0) {
add = Heap[1];
while(nHeap > 0 && Heap[1] == add) Pop();
}
}
}
int k = 0;
while(nHeap > 0) {
k++;
a[k] = Heap[1];
while(nHeap > 0 && a[k] == Heap[1]) Pop();
}
cout << k << endl;
for(int i = 1; i <= k; i++) cout << a[i] << endl;
}