-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path10107.cpp
More file actions
48 lines (47 loc) · 1.42 KB
/
Copy path10107.cpp
File metadata and controls
48 lines (47 loc) · 1.42 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
// Author: btjanaka (Bryon Tjanaka)
// Problem: (UVa) 10107
#include <bits/stdc++.h>
#define GET(x) scanf("%d", &x)
#define GED(x) scanf("%lf", &x)
typedef long long ll;
using namespace std;
typedef pair<int, int> ii;
int main() {
// two heaps, balance the incoming numbers between them
int n;
priority_queue<int, vector<int>, less<int>> highest_first;
priority_queue<int, vector<int>, greater<int>> lowest_first;
while (GET(n) > 0) {
if (highest_first.size() == lowest_first.size()) {
if (highest_first.empty()) {
highest_first.push(n);
} else if (n >= highest_first.top()) {
lowest_first.push(n);
highest_first.push(lowest_first.top());
lowest_first.pop();
} else {
highest_first.push(n);
}
printf("%d\n", highest_first.top());
} else if (highest_first.size() > lowest_first.size()) {
if (n >= highest_first.top()) {
lowest_first.push(n);
} else {
highest_first.push(n);
lowest_first.push(highest_first.top());
highest_first.pop();
}
printf("%d\n", (highest_first.top() + lowest_first.top()) / 2);
} else {
if (n <= lowest_first.top()) {
highest_first.push(n);
} else {
lowest_first.push(n);
highest_first.push(lowest_first.top());
lowest_first.pop();
}
printf("%d\n", (highest_first.top() + lowest_first.top()) / 2);
}
}
return 0;
}