Created
November 7, 2017 02:49
-
-
Save sashang/4be2c38ba699ab3b278ddac91f5dba77 to your computer and use it in GitHub Desktop.
median tracking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <functional> | |
#include <queue> | |
using namespace std; | |
int main(int argc, char** argv) | |
{ | |
priority_queue<int, vector<int>, function<bool(int,int)>> | |
minq([](int a, int b)->bool{return a > b;}); | |
priority_queue<int, vector<int>, function<bool(int,int)>> | |
maxq([](int a, int b) -> bool { return a < b; }); | |
while (cin.good()) | |
{ | |
int x; | |
cin >> x; | |
if (minq.empty() && maxq.empty()) | |
{ | |
minq.emplace(x); | |
continue; | |
} | |
if (x > minq.top()) | |
{ | |
minq.emplace(x); | |
} | |
else | |
{ | |
maxq.emplace(x); | |
} | |
//balance queues | |
//when total number of elements is odd, minq will contain the overflow. | |
if (minq.size() > maxq.size() + 1) | |
{ | |
maxq.emplace(minq.top()); | |
minq.pop(); | |
} | |
else if (maxq.size() > minq.size()) | |
{ | |
minq.emplace(maxq.top()); | |
maxq.pop(); | |
} | |
cout << "median is " << (minq.size() == maxq.size() ? ((minq.top() + maxq.top()) * 0.5) : minq.top()) << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment