【优先队列】HDU4006The kth great number

来源:互联网 发布:linux ssh暴力破解 编辑:程序博客网 时间:2024/06/05 02:49

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4006

Problem Description
Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to write down a number, or ask Xiao Bao what the kth great number is. Because the number written by Xiao Ming is too much, Xiao Bao is feeling giddy. Now, try to help Xiao Bao.
 

Input
There are several test cases. For each test case, the first line of input contains two positive integer n, k. Then n lines follow. If Xiao Ming choose to write down a number, there will be an " I" followed by a number that Xiao Ming will write down. If Xiao Ming choose to ask Xiao Bao, there will be a "Q", then you need to output the kth great number. 
 

Output
The output consists of one integer representing the largest number of islands that all lie on one line. 
 

Sample Input
8 3I 1I 2I 3QI 5QI 4Q

题目意思:

给出一个n和一个k,n为有多少条命令,命令分为两种,输入‘I’时代表增加一个数,输入‘Q’代表询问并输出第k大的数

这个题目可以用很多种方法,线段树,树状数组,小根堆,and so on......

代码:

#include<iostream>#include<algorithm>#include<queue>using namespace std;int b[1000005];int main(){    int n,k,a;    char s;    cin.sync_with_stdio(false);    while(cin>>n>>k){        priority_queue<int,vector<int>,greater<int> >q;     //  从大到小排序;        while(n--){            cin>>s;            if(s=='I'){                cin>>a;                if(q.size()<k) q.push(a);                else if(q.top()<a){                    q.pop();                    q.push(a);                }            }else cout<<q.top()<<endl;        }    }    /*          直接暴力TLE    while(cin>>n>>k){        int cnt=0;        while(n--){            cin>>s;            if(s=='I'){                cin>>a;                if(cnt<k) b[cnt++]=a,sort(b,b+cnt);                else if(b[0]<a){                    b[0]=a;                    sort(b,b+k);                }            }else cout<<b[0]<<endl;        }    }    */    return 0;}


0 0
原创粉丝点击