36分
查看原帖
36分
934631
Perry6632楼主2024/8/15 09:04
#include<iostream>
using namespace std;
//堆
class heap_{
    private:
        int heap[1000001]={},siz=0;
        int f(int x){
            return (x-1)/2;
        }
        int l(int x){
            return x*2+1;
        }
        int r(int x){
            return x*2+2;
        }
        void swap(int &x,int &y){
            int t=x;
            x=y;
            y=t;
        }
        bool cmp(int x,int y){
            if(x>y)
                return 1;
            else
                return 0;
        }
    public:
        void push(int x){
            int p=siz;
            heap[siz++]=x;
            while(p>0){
                if(cmp(heap[f(p)],heap[p]))
                    swap(heap[f(p)],heap[p]);
                else 
                    break;
                p=f(p);
            }
        }
        void pop(){
            int p=0;
            swap(heap[--siz],heap[0]);
            while(f(p)<siz){
                int t;
                if(cmp(heap[l(p)],heap[r(p)]))
                    t=r(p);
                else
                    t=l(p);
                if(cmp(heap[t],heap[p]))
                    swap(heap[t],heap[p]);
                else
                    break;
                p=t;
            }
        }
        int front(){
            return heap[0];
        }
        int size(){
            return siz;
        }
        bool empty(){
            return siz==0;
        }
        void f(){
            for(int i=0;i<siz;i++)
                cout<<heap[i]<<endl;
        }
};
heap_ h;
int main(){
    int n,a,b;
    cin>>n;
    for (int i=0;i<n;i++){
        cin>>a;
        if(a==1){
            cin >> b;
            h.push(b);
        }
        else if(a==2){
            cout<<h.front()<<endl;
        }
        else if(a==3){
            h.pop();
        }
    }
}
2024/8/15 09:04
加载中...