hdu4006

链接:点击打开链接

题意:给出一个n和一个k,n为有多少条命令,命令分为两种,输入‘I’时代表增加一个数,输入‘Q’代表询问并输出第k大的数

代码:

#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;
int main(){
    int i,n,k,num;
    char c;
    priority_queue<int,vector<int>,greater<int> >q;
    while(scanf("%d%d",&n,&k)!=EOF){
        while(q.size())q.pop();             //初始化队列
        for(i=1;i<=n;i++){
        cin>>c;
        if(c=='I'){
            scanf("%d",&num);
            if(q.size()<k)                  //永远使队首为所求的值
            q.push(num);
            else if(q.top()<num)            //当队首小于输入值时,所求的
            q.pop(),q.push(num);            //值的大小一定发生变化,因此
        }                                   //q.pop()
        else
        printf("%d\n",q.top());             //运用优先队列使得队首永远是所求值
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-05 19:38:11

hdu4006的相关文章

hdu4006 优先队列

A - 签到 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4006 Description Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can

HDU The kth great number 优先队列、平衡树模板题(SBT)

The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) [Problem Description] Xiao  Ming  and  Xiao  Bao  are  playing  a  simple  Numbers  game.  In  a  round  Xiao  Ming  can choose  to  write  down 

堆,二分,尺取

A - Aggressive cows POJ - 2456 Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000). His C (2 <= C <= N) cows don't like