模板 - 数据结构 - 对顶堆

#include<bits/stdc++.h>
using namespace std;

struct Opposite_Heap{
    //top_heap has the min element,bottom heap has the max element

    priority_queue<int,vector<int>,less<int> > top_heap;
    priority_queue<int,vector<int>,greater<int> > bottom_heap;

    int k;

    Opposite(){k=0;}

    void add(int value){
        /*change k*/
        if(top_heap.size()<=i){
            top_heap.push(value);
        }
        else{
            if(value<top_heap.top()){
                bottom_heap.push(top_heap.top());
                top_heap.pop();
                top_heap.push(value);
            }
            else{
                bottom_heap.push(value);
            }
        }
        /*change k*/
    }

    int get(){
        /*change k*/
        /*while(top_heap.size()<=k&&!bottom_heap.empty()){
            top_heap.push(bottom_heap.top());
            bottom_heap.pop();
        }*/

        int t=top_heap.top();

        /*change k*/
        while(top_heap.size()<=k&&!bottom_heap.empty()){
            top_heap.push(bottom_heap.top());
            bottom_heap.pop();
        }
        return t;
    }
};

猜你喜欢

转载自www.cnblogs.com/Yinku/p/10498815.html