首页 > 其他 > 详细

模板 - 数据结构 - 对顶堆

时间:2019-03-08 23:36:50      阅读:268      评论:0      收藏:0      [点我收藏+]
#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;
    }
};

 

模板 - 数据结构 - 对顶堆

原文:https://www.cnblogs.com/Yinku/p/10498815.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!