HDOJ 4006 The kth great number

The kth great number

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65768/65768K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size:  

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 3
I 1
I 2
I 3
Q
I 5
Q
I 4
Q

Sample Output

1
2
3

Hint

Xiao Ming won't ask Xiao Bao the kth great number when the number of the written number is smaller than k. (1=<k<=n<=1000000).

题意,输入命令个数,和相应第k大的数,每次I命令,向集合中添加一个数字,每次Q命令
输出第k大的数是哪一个。可以用优先队列模拟,每当插入一个数字,且当时的队列元素个数
大于k时,则将一个小的元素出队,这样,可以保证队首元素一直是第k大的数字。

#include<iostream>
#include<string>
#include<queue>
using namespace std;
struct node
{
    int val;
    node(int _val):val(_val){}
    node(){}
    friend bool operator <(node a,node b){
        return a.val>b.val;
    }
};
int main()
{
    int m,n,x;
    string cmd;
    while(cin>>m>>n)
    {
        priority_queue<node>q;
        while(m--)
        {
            cin>>cmd;
            if(cmd=="I"){
                cin>>x;
                q.push(node(x));
                if(q.size()>n)
                    q.pop();
            }
            else if(cmd=="Q")
                cout<<q.top().val<<endl;
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-04-24 17:54  Thereisnospon  阅读(109)  评论(0编辑  收藏  举报