The kth great number/hdoj 4006 2011 Regional Dalian Online priority queue

      We can use priority queue to support the question.Firstly ,we push k number into priority queue.  Then ,if the number bigger than the top number of the priority queue,push the number into the priority queue,or  throw   out. If input 'Q',output the top

number of prority queue.



#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int flag[1000010];
int main()
{
    int n;
    char str[10];
    int num;
    int i;
    int k;
    while(scanf("%d %d",&n,&k)!=EOF)
    {
        //priority_queue<int,vector<int>,less<int> > qu;

        priority_queue<int,vector<int>,greater<int> > qu;
        
        for(i=1;i<=n;i++)
        {
            scanf("%s",str);
            if(str[0]=='I')
            {
                scanf("%d",&num);

                if(i>k)
                {
                    if(num>qu.top())
                    {
                        qu.pop();
                        qu.push(num);
                    }
                    
                }
                else
                {

                    qu.push(num);
                }
            }
            else if(str[0]=='Q')
            {
                printf("%d\n",qu.top());
            }
        }
    }
    return 0;
}


你可能感兴趣的:(The kth great number/hdoj 4006 2011 Regional Dalian Online priority queue)