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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章