http://acm.hdu.edu.cn/showproblem.php?pid=4006
View Code
1 #include<queue> 2 #include<stdio.h> 3 #include<string.h> 4 #include<functional> 5 #include<vector> 6 using namespace std; 7 struct cmp 8 { 9 bool operator()(const int &a,const int &b) 10 { 11 return a>b;//从小到大 12 } 13 }; 14 int main() 15 { 16 int n,k,m; 17 char ch; 18 19 while(~scanf("%d%d",&n,&k)) 20 { 21 priority_queue<int,vector<int>,cmp>qi; 22 for(int i=0;i<n;i++) 23 { 24 getchar(); 25 scanf("%c",&ch); 26 if(ch=='I') 27 { 28 scanf("%d",&m); 29 qi.push(m); 30 if(qi.size()>k) qi.pop(); 31 } 32 else printf("%d\n",qi.top()); 33 } 34 } 35 }
View Code
1 #include<queue> 2 #include<stdio.h> 3 #include<string.h> 4 #include<functional> 5 #include<vector> 6 using namespace std; 7 int main() 8 { 9 int n,k,m; 10 char ch; 11 12 while(~scanf("%d%d",&n,&k)) 13 { 14 priority_queue<int,vector<int>,greater<int> >qi; 15 for(int i=0;i<n;i++) 16 { 17 getchar(); 18 scanf("%c",&ch); 19 if(ch=='I') 20 { 21 scanf("%d",&m); 22 qi.push(m); 23 if(qi.size()>k) qi.pop(); 24 } 25 else printf("%d\n",qi.top()); 26 } 27 } 28 }