The kth great number

The kth great number

Time Limit: 1000ms
Memory Limit: 65768KB
64-bit integer IO format: %I64d      Java class name: Main
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).
 
题目大意:第一行输入N,M,然后有N行,输入操作数,查找第M大的数,I 表示插入元素,Q表示询问在你插入的数种第M大的数是多少,采用优先队列可即可;
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string>
 4 #include <queue>
 5 #include <stdio.h>
 6 using namespace std;
 7 struct Node
 8 {
 9     int x;
10     friend bool operator < (Node a,Node b)/*重载小于号,设置新的优先级*/
11     {
12         return a.x > b.x;
13     }
14 };
15 int main()
16 {
17     int T,k;
18     char Str;
19     int SIGN;
20     while(scanf("%d%d",&T,&k)!=EOF)
21     {
22         SIGN = 0;
23         priority_queue <Node> q;
24         while(T--)
25         {
26             scanf(" %c",&Str);
27             Node num;
28             if(Str == 'I')
29             {
30                 if(SIGN < k)    /*若输入K个数后,只存K个数*/
31                 {               /*经过小于号重载,队头元素则是第K大*/
32                     scanf("%d",&num.x);
33                     q.push(num);
34                     SIGN++;
35                 }
36                 else
37                 {
38                     scanf("%d",&num.x);
39                     if(num.x>q.top().x)/*判断输入的数比队头元素大*/
40                     {                 
41                         q.pop();       /*队头元素出队列,*/
42                         q.push(num);    /*把该数放入优先队列中*/
43                     }
44                 }
45             }
46             else
47             {
48                 printf("%d\n",q.top().x);/*输出队头元素则是第K大的数 */
49             }
50         }
51     }
52     return 0;
53 }
View Code

 

posted @ 2015-05-10 20:52  Wurq  阅读(181)  评论(0)    收藏  举报