The Meanings Of Problems:
现在有两种操作,一个是读入数字(以I开头),另外一个是查询(以Q开头),查询当前第K大的数字。
Objects Categories:
模拟题,因为n和k的数目都很大,建议使用priority_queue。
Algorithms Descriptions:
Input: n,k,operations
Output: the k-th number
- input{n,k};
- consturctor{priority_queue:p_que};
- for 1 to n
- read(opertions);
- if operation is I-operations then
- read(number);
- enqueue(number);
- if the length of p_que < =k then
- continue;
- else
- popqueue;
- endif
- else
- topqueue;
- endif
- endfor
Codes:
#include<iostream>
#include<queue>
#include<functional>
using namespace std;
int main(){
priority_queue<int,vector<int>,greater<int> > arrays;
int num,k;
char ch;
int digit;
while(cin>>num>>k){
while(!arrays.empty()){
arrays.pop();
}
for(int i=0;i<num;i++){
cin>>ch;
if(ch=='I'){
cin>>digit;
arrays.push(digit);
if(arrays.size()>k){
arrays.pop();
}
}else{
cout<<arrays.top()<<endl;
}
}
}
return 0;
}