-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva10107.cpp
More file actions
56 lines (50 loc) · 1.11 KB
/
uva10107.cpp
File metadata and controls
56 lines (50 loc) · 1.11 KB
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
#include<iostream>
#include<queue>
#include<vector>
#include<sstream>
#include<string>
using namespace std;
class MedianTracker {
priority_queue<int, vector<int>, greater<int>> p2;
priority_queue<int> p1;
public:
void addItem(int item) {
if (p1.size() and item > p1.top()) {
p2.push(item);
} else {
p1.push(item);
}
if (p1.size() > p2.size() + 1) {
p2.push(p1.top());
p1.pop();
}
if (p2.size() > p1.size() + 1) {
p1.push(p2.top());
p2.pop();
}
}
int getMedian() {
if (p1.size() > p2.size()) {
return p1.top();
} else if (p2.size() > p1.size()) {
return p2.top();
} else {
return (p1.top() + p2.top())/2;
}
}
};
int main() {
MedianTracker m;
istringstream iss;
string s;
int n;
while(true) {
getline(cin, s);
if (s.length() == 0)
break;
iss = istringstream(s);
iss >> n;
m.addItem(n);
cout << m.getMedian() << endl;
}
}