Last active
October 7, 2015 03:37
-
-
Save osjayaprakash/3098716 to your computer and use it in GitHub Desktop.
Kthe Largest Element
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 <vector> | |
#include <stack> | |
#include <queue> | |
#include <algorithm> | |
#include <cstdio> | |
using namespace std; | |
struct compare { | |
bool operator()(const int& l, const int& r) { | |
return l > r; | |
} | |
}; | |
priority_queue <int, vector<int>, compare> m; | |
priority_queue <int> s; | |
int N; | |
int main() { | |
int nq,q,x; | |
scanf("%d", &nq); | |
while(nq--){ | |
scanf("%d", &q); | |
if (q == 2) { | |
while (m.size() < N/3) { | |
m.push(s.top()); | |
s.pop(); | |
} | |
while (m.size() > N/3) { | |
s.push(m.top()); | |
m.pop(); | |
} | |
if (m.size() == 0) | |
printf("No reviews yet\n"); | |
else | |
printf("%d\n", m.top()); | |
} else if (q==1) { | |
scanf("%d", &x); | |
if (m.size() && x > m.top()) | |
m.push(x); | |
else | |
s.push(x); | |
N++; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment