为什么后面这个可以删堆是错的
  • 板块学术版
  • 楼主cjwen
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/9/12 10:15
  • 上次更新2024/9/12 10:29:28
查看原帖
为什么后面这个可以删堆是错的
367296
cjwen楼主2024/9/12 10:15
struct KSD{
    priority_queue<node> q1, q2;

    void push(node x){
        q1.push(x);
    }

    void del(node x){
        q2.push(x);
    }

    void doit(){
        while(!q2.empty() && q1.top()==q2.top()){
            q1.pop();
            q2.pop();
        }
    }

    bool empty(){
        doit();
        return q1.empty();
    }

    node top(){
        doit();
        return q1.top();
    }
};

struct KSD2{
    multiset<node> st;

    void push(node x){
        st.insert(x);
    }

    void del(node x){
        st.erase(st.find(x));
    }

    bool empty(){
        return st.empty();
    }

    node top(){
        return *(st.begin());
    }
};

前面一种可以,后面一种不行,为什么?

2024/9/12 10:15
加载中...