-
Notifications
You must be signed in to change notification settings - Fork 0
/
medianheap.cpp
53 lines (47 loc) · 1.23 KB
/
medianheap.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
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
priority_queue<int, vector<int>, greater<int>> minheap;
priority_queue<int> maxheap;
int n, x, a, b; cin >> n;
string s;
while(n--){
cin >> s;
if (s=="PUSH") {
cin >> x;
if (maxheap.size() == 0) maxheap.push(x);
else if (x <= maxheap.top()) maxheap.push(x);
else minheap.push(x);
}
else {
maxheap.pop();
a = minheap.size(), b = maxheap.size();
if (a - b > 0) {
maxheap.push(minheap.top());
minheap.pop();
}
}
while (minheap.size() > maxheap.size()){
maxheap.push(minheap.top());
minheap.pop();
}
while (maxheap.size() > minheap.size() + 1){
minheap.push(maxheap.top());
maxheap.pop();
}
}
vector<int> v;
while (maxheap.size() > 0) {
v.push_back(maxheap.top());
maxheap.pop();
}
while (minheap.size() > 0) {
v.push_back(minheap.top());
minheap.pop();
}
sort(v.begin(), v.end());
for (auto c : v) cout << c << ' ';
}