44分 求助!!!
查看原帖
44分 求助!!!
921302
wswyx楼主2023/3/7 11:31
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int p[N],idx=0;
void down(int m)
{
    int u=m;
    if(2*m<=idx&&p[u]>p[2*m]) u=2*m;
    if(2*m+1<=idx&&p[u]>p[2*m+1]) u=2*m+1;
    if(m!=u){
        swap(p[m],p[u]);
        down(m);
        }
}
void up(int u)
{
    while (u / 2 && p[u] < p[u / 2])
    {
        swap(p[u],p[u / 2]);
        u/=2;
    }
}
int main()
{
    int n;
    cin>>n;
    while(n--){
        int a;
        cin>>a;
        if(a==1){
          int b;  
            cin>>b;
            p[++idx]=b;
            up(idx);
            down(idx);
        }
        else if(a==2) cout<<p[1]<<endl;
        else{
            p[1]=p[idx];
            idx--;
            down(1);
        }
    }
    
    return 0;
}
    
2023/3/7 11:31
加载中...