现在的位置: 首页 > 综合 > 正文

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

2017年10月18日 ⁄ 综合 ⁄ 共 638字 ⁄ 字号 评论关闭

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

在此曝上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;
}

抱歉!评论已关闭.