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。

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