The 36th ACM/ICPC Asia Regional Dalian Site 1006 The kth great number

大连的网络赛悲剧了,没有出线,看来还得继续努力呀!!奋斗!

在此曝上1006的解题报告,我用优先队列做的,看来STL是超有用呀,提醒做ACM的要掌握好STL呀^_^~~~~


#include <algorithm>
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <map>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
int main()
{
    int n,k;
    while(scanf("%d%d",&n,&k)!=EOF){
    priority_queue< int ,vector < int >,greater<int> > q;

    int i;
    for(i=0;i<n;i++){
        char c;
        getchar();
        c = getchar();
        if(c=='I')
        {
            int g;
            scanf("%d",&g);
            if(q.empty())
            {
                q.push(g);
            }
            else
            {
                if(q.size()<k)q.push(g);
                else
                {//维持队列的大小始终为k
                    int kk=q.top();
                    if(g>=kk)//比当前的第k大数大的数留下,其他的丢掉
                    {
                     q.pop();
                     q.push(g);
                    }

                }

            }
        }
        else if(c=='Q')
        {
            int r=q.top();//输出队列中的最小值,即为答案
            printf("%d\n",r);
        }
    }
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值