Skip to content

Instantly share code, notes, and snippets.

@aliciawyy
Created July 28, 2019 21:35
Show Gist options
  • Save aliciawyy/2585fa5102364560287d6b53bf538b37 to your computer and use it in GitHub Desktop.
Save aliciawyy/2585fa5102364560287d6b53bf538b37 to your computer and use it in GitHub Desktop.
UVa 10107 What is the median
#include <bits/stdc++.h>
using namespace std;
int main() {
int m;
bool is_from_max_heap = true;
priority_queue<int, vector<int>> max_heap;
priority_queue<int, vector<int>, greater<int>> min_heap;
while (cin >> m) {
max_heap.emplace(m);
min_heap.emplace(max_heap.top());
max_heap.pop();
if (is_from_max_heap) {
max_heap.emplace(min_heap.top());
min_heap.pop();
cout << max_heap.top() << endl;
} else {
cout << (min_heap.top() + max_heap.top()) / 2 << endl;
}
is_from_max_heap = !is_from_max_heap;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment