O(1) 复杂度支持任意位置删除的 priority_queue 实现

问题由来

我们知道 priority_queue 内部缺省是使用 vector 来实现的,而 vector 是不支持任意位置删除,只能通过迭代器删除。如果我们要实现 O(1) 复杂度,支持任意位置删除,该怎么办?

思路

priority_queue 只能删除顶部(top)。我们可以利用这个特性,保存两个 priority_queue,一个是数据,一个是待删除数据,当两个 priority_queue 的 top() 元素相同的时候,我们再删除两个优先队列的 top。

实现

升序优先队列

实现代码

typedef struct _MAXHEAP {
    priority_queue<int> Q,D;
    void push(int x) {Q.push(x);}
    void erase(int x) {D.push(x);}
    bool empty() {
        return Q.empty();
    }
    int top() {
        while(!D.empty()&&Q.top()==D.top()) {Q.pop();D.pop();}
        return Q.empty()?0:Q.top();
    }
} MAXHEAP;

上面的代码中,MAXHEAP 内部有两个优先队列,其中 Q 队列保存当前元素,D 队列保存需要删除的元素。

测试代码

有三种操作:

1、1 是插入指定元素。例如 1 3,表示插入元素 3。

2、2 是删除指定元素。例如 2 3,表示删除元素 3。

3、3 是获取优先队列的 top 值。

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

using namespace std;

typedef struct _MAXHEAP {
    priority_queue<int> Q,D;
    void push(int x) {Q.push(x);}
    void erase(int x) {D.push(x);}
    bool empty() {
        return Q.empty();
    }
    int top() {
        while(!D.empty()&&Q.top()==D.top()) {Q.pop();D.pop();}
        return Q.empty()?0:Q.top();
    }
} MAXHEAP;

int main() {
    int type,x;
    MAXHEAP pq;
    while (1) {
        scanf("%d",&type);
        if (1==type) {
           scanf("%d",&x);
           pq.push(x);
        } else if (2==type) {
            scanf("%d",&x);
            pq.erase(x);
        } else if(3==type) {
            if (pq.empty()) {
                printf("max -1\n");
            } else {
                printf("max %d\n",pq.top() );
            }
        }
    }
    return 0;
}

测试结果如下图。

降序优先队列

实现代码

typedef struct _MINHEAP {
    priority_queue<int, vector<int>, greater<int> > Q,D;
    void push(int x) {Q.push(x);}
    void erase(int x) {D.push(x);}
    bool empty() {
        return Q.empty();
    }
    int top() {
        while(!D.empty()&&Q.top()==D.top()) {Q.pop();D.pop();}
        return Q.empty()?0:Q.top();
    }
} MINHEAP;

测试代码

有三种操作:

1、1 是插入指定元素。例如 1 3,表示插入元素 3。

2、2 是删除指定元素。例如 2 3,表示删除元素 3。

3、3 是获取优先队列的 top 值。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>

using namespace std;

typedef struct _MINHEAP {
    priority_queue<int, vector<int>, greater<int> > Q,D;
    void push(int x) {Q.push(x);}
    void erase(int x) {D.push(x);}
    bool empty() {
        return Q.empty();
    }
    int top() {
        while(!D.empty()&&Q.top()==D.top()) {Q.pop();D.pop();}
        return Q.empty()?0:Q.top();
    }
} MINHEAP;

int main() {
    int type,x;
    MINHEAP pq;
    while (1) {
        scanf("%d",&type);
        if (1==type) {
           scanf("%d",&x);
           pq.push(x);
        } else if (2==type) {
            scanf("%d",&x);
            pq.erase(x);
        } else if(3==type) {
            if (pq.empty()) {
                printf("max -1\n");
            } else {
                printf("max %d\n",pq.top() );
            }
        }
    }
    return 0;
}

测试结果如下图。

总结

如上面的两个运行结果图,我们实现了一个 O(1) 复杂度的任意位置删除的 priority_queue。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值