可喜可贺!终于按照自己的想法A了第一道优先队列题,虽然WA了4次,但还是很开心。
刚开始想输出第三大数,那每次输出的时候都把第k个以前的都存起来出队列,输出第k个后再入队列回去,后来果断超时(噗)。
然后就想干脆只弄k个有效值,队列满了k个就和队首判断然后输出,中间还有几次失误,不过还好A了~
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
const int N = 5000;
const int INF = 1000000;
struct node
{
int w;
friend bool operator < (const node &a, const node &b)
{
return a.w > b.w;
}
};
int main()
{
// freopen("in.txt", "r", stdin);
char a[3];
int n, k, num;
while(~scanf("%d%d", &n, &k))
{
priority_queue <node> q;
while(n --)
{
cin >> a;
if(a[0] == 'I')
{
if(q.size() >= k)//等于不可去掉
{
node jud = q.top();
scanf("%d", &num);
node tmp;
tmp.w = num;
if(tmp.w > jud.w)
{
q.push(tmp);
q.pop();
}
}
else
{
node tmp;
scanf("%d", &num);
tmp.w = num;
q.push(tmp);
}
}
else if(a[0] == 'Q')
{
node tmp1 = q.top();
printf("%d\n", tmp1.w);
}
}
}
return 0;
}