思路:用优先队列维护前k个大的元素,每次查询时,队首元素即为所求答案。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
priority_queue<int, vector<int>, greater<int> > q;
int main()
{
int n, k;
while (scanf("%d %d", &n, &k) != EOF) {
while (!q.empty()) q.pop();
int i, val;
char ch;
for (i = 0; i < n; ++i) {
getchar();
scanf("%c", &ch);
if (ch == 'I') {
scanf("%d", &val);
q.push(val);
}
else {
while ((int)q.size() > k) q.pop();
val = q.top();
printf("%d\n", val);
}
}
}
return 0;
}