分析:可以用线段树做,但感觉麻烦。用优先队列,每次插入时都保留前k个大的数即可。
#include<iostream> #include<functional> #include<queue> using namespace std; int main() { int n,k,j,c; char b[2]; ios::sync_with_stdio(false); while(cin>>n>>k) { priority_queue<int,vector<int>,greater<int> > q; for(j=0;j<n;j++) { if((scanf("%s",b),b[0])=='I') { q.push((cin>>c,c)); if(q.size()>k) q.pop(); } else { cout<<q.top()<<endl; } } } return 0; }
HDU ACM 4006 The kth great number 线段树?优先队列?
原文地址:http://blog.csdn.net/a809146548/article/details/45620641