HDU--4006(The kth great number)

The Meanings Of Problems:

    现在有两种操作,一个是读入数字(以I开头),另外一个是查询(以Q开头),查询当前第K大的数字。

Objects Categories:

    模拟题,因为n和k的数目都很大,建议使用priority_queue。

Algorithms Descriptions:

     Input: n,k,operations

    Output: the k-th number

  1.     input{n,k};
  2.    consturctor{priority_queue:p_que};
  3.    for 1 to n
  4.        read(opertions);
  5.       if operation is I-operations then
  6.              read(number);
  7.              enqueue(number);
  8.              if the length of p_que < =k then
  9.                    continue;
  10.             else
  11.                   popqueue;
  12.              endif
  13.        else 
  14.               topqueue;
  15.      endif
  16.    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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值