HDU 4006 The kth great number

动态求第k大的数,只需维护一个长度为k的优先队列

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
int main(){
    int n,k;
    while(scanf("%d%d",&n,&k)!=EOF){
    	priority_queue, greater > pq;
        int num;
        char ch;
        int cnt=0;
        while(n--){
            getchar();
            scanf("%c",&ch);
            if(ch=='I'){
                scanf("%d",&num);
                cnt++;
                if(cnt<=k){
                    pq.push(num);
                }
                else{
                    if(num>pq.top()){
                        pq.pop();
                        pq.push(num);
                    }
                }
            }
            else{
                printf("%d\n",pq.top());
            }
        }
    }
	return 0;
}

你可能感兴趣的:(stl)