-
Notifications
You must be signed in to change notification settings - Fork 0
/
1655.cpp
50 lines (39 loc) · 785 Bytes
/
1655.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
#include <queue>
#include <vector>
#include <iostream>
#include <functional>
using namespace std;
void swap(int* a, int* b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
priority_queue<int> max_heap;
priority_queue<int, vector<int>, greater<int>> min_heap;
int tc;
cin >> tc;
while (tc--) {
int x; cin >> x;
if (max_heap.size() == min_heap.size()) {
max_heap.push(x);
}
else {
min_heap.push(x);
}
if (!max_heap.empty() && !min_heap.empty()
&& max_heap.top() > min_heap.top()) {
int a = max_heap.top();
int b = min_heap.top();
swap(&a, &b);
max_heap.pop();
min_heap.pop();
max_heap.push(a);
min_heap.push(b);
}
cout << max_heap.top() << '\n';
}
return 0;
}