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。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章