hdu 4006 第K大的数(优先队列)

N次操作 I是插入一个数 Q是输出第K大的数

Sample Input
8 3 //n k
I 1
I 2
I 3
Q
I 5
Q
I 4
Q

Sample Output
1
2
3

 

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <string>
 6 # include <cmath>
 7 # include <queue>
 8 # include <list>
 9 # define LL long long
10 using namespace std ;
11 
12 struct ss
13 {
14     friend bool operator<(const ss a,const ss b)
15     {
16         if(a.v>b.v)
17             return 1;
18         else
19             return 0;
20     }
21     int v;
22 };
23 
24 int main()
25 {
26     //freopen("in.txt","r",stdin) ;
27     int n , k ;
28     char s[10];
29     while(scanf("%d %d" , &n , &k) != EOF)
30     {
31         priority_queue<ss> q ;
32         ss t;
33         while(n--)
34         {
35             scanf("%s",s);
36             if(s[0]=='I')
37             {
38                 int a;
39                 scanf("%d",&a);
40                 t.v=a;
41                 q.push(t);
42                 if(q.size()>k)
43                 {
44                     q.pop();
45                 }
46             }
47             else
48             {
49                 printf("%d\n",q.top());
50             }
51         }
52     }
53 
54     return 0;
55 }
View Code

 

posted @ 2015-09-24 20:05  __Meng  阅读(332)  评论(0编辑  收藏  举报