HDOJ 4006 - The kth great number 优先队列水

本文介绍了一种利用优先队列(大根堆)实时处理数据流并快速找到当前数据集中第K大的数值的方法。通过不断更新优先队列中的元素数量来确保始终能够返回正确的结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

         题意:

                     不断的往集合里放数..又有不断的询问集合中第k大的数是什么...

         题解:   

                     本来想用treap水一下的...结果WA了...treap应该水得过吧???模板有错~~~汗颜.....那就直接优先队列水把...维护点个数为k的一个大根堆就行了...


Program:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<time.h>
#include<map>
#include<algorithm>
#define ll long long
#define eps 1e-5
#define oo 1000000007
#define pi acos(-1.0)
#define MAXN 2000005
using namespace std;  
struct node
{
       int x;
       bool operator <(node a) const
       {
              return a.x<x;
       }
}h,p;
priority_queue<node> myqueue;
int main()
{               
       int m,k;      
       while (~scanf("%d%d\n",&m,&k))
       {
                while (!myqueue.empty()) myqueue.pop();
                while (m--)
                {
                        char c;
                        do { c=getchar(); } while (c<'A' || c>'Z');
                        if (c=='I') 
                        {
                            int x;
                            scanf("%d",&x);
                            h.x=x;
                            myqueue.push(h);
                            if (myqueue.size()>k) myqueue.pop();
                        }else 
                            printf("%d\n",myqueue.top().x);
                }
       }
       return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值