2015
04-14

# The kth great number

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.

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.

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.

8 3
I 1
I 2
I 3
Q
I 5
Q
I 4
Q

1
2
3

HintXiao  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). 

/*

据说有很多方法，不过如果想省事儿的话，

2012-07-27
*/

#include"stdio.h"
#include"queue"
using namespace std;

struct node
{
int num;
friend bool operator<(node n1,node n2)
{
return n2.num<n1.num;
}
};

int main()
{
node cur,next;
int n,k;
int tot;
char str[5];

while(scanf("%d%d",&n,&k)!=-1)
{
priority_queue<node>q;
tot=0;
while(n--)
{
scanf("%s",str);
if(str[0]=='Q')
{
cur=q.top();
printf("%d\n",cur.num);
}
else
{
scanf("%d",&next.num);
if(tot<k)	{q.push(next);tot++;}
else
{
cur=q.top();
if(next.num>cur.num)
{
q.pop();
q.push(next);
}
}
}
}
}

return 0;
}

1. Thanks for using the time to examine this, I truly feel strongly about it and enjoy finding out far more on this subject matter. If achievable, as you achieve knowledge

2. [email protected]