黑盒子【对顶堆】

https://www.acwing.com/activity/content/problem/content/397/1/

我们的大根堆中的所有数都必须小于小根堆中的数。而且还要注意,i每一次都是要增加1的,所以我们每次做完Get操作后,都需要最小堆中的最小数弹出存入最大堆,也就是满足第K小,因为K就是i,所以我们要这么处理。
俩个堆维护一个有序序列,求第k个小的数


    #include <iostream>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;

const int N = 30010;

int n, m;
int a[N], b[N];

int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i ++ ) cin >> a[i];
    for (int i = 1; i <= m; i ++ ) cin >> b[i];

    priority_queue<int> down;
    priority_queue<int, vector<int>, greater<int>> up;

    int k = 0;
    for (int i = 1, j = 1; i <= m; i ++ )
    {
        while (j <= b[i])
        {
            if (down.empty() || a[j] >= down.top()) up.push(a[j]);
            else
            {
                up.push(down.top());
                down.pop();
                down.push(a[j]);
            }
            j ++ ;
        }

        down.push(up.top());
        up.pop();
        cout << down.top() << endl;
    }

    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值