-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10107.cpp
40 lines (37 loc) · 969 Bytes
/
P10107.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
#include <iostream>
#include <set>
typedef unsigned long ul;
typedef std::multiset<ul> set;
int main() {
set low, high; // |high| <= |low|+1, |low| <= |high|+1
ul X;
while(std::cin >> X) {
if(high.empty() || X > *high.begin()) {
high.insert(X);
if(high.size() > low.size()+1) {
int mid = *high.begin();
high.erase(high.begin());
low.insert(mid);
}
}
else {
low.insert(X);
if(low.size() > high.size()) {
int mid = *low.rbegin();
set::iterator it = low.end();
--it;
low.erase(it);
high.insert(mid);
}
}
if(high.size() == low.size()) {
//std::cerr << "|low|==|hig|, |low| = " << low.size() << ", |high|=" << high.size() << std::endl;
std::cout << (*low.rbegin() + *high.begin())/2 << std::endl;
}
else {
//std::cerr << "|low|!=|hig|, |low| = " << low.size() << ", |high|=" << high.size() << std::endl;
std::cout << *high.begin() << std::endl;
}
}
return 0;
}