全WA 求调

P1168 中位数

K__J__M @ 2023-12-02 16:05:15

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5+10;
int n,mid,a[N];
int heap_max[N],heap_min[N],heap_size_min,heap_size_max;
void push_min(int x){
    heap_min[++heap_size_min]=x;
    int now=heap_size_min;
    while(now>>1){
        int father=now>>1;
        if(heap_min[father]>heap_min[now]) swap(heap_min[father],heap_min[now]);
        else break;
        now=father;
    }
}
void push_max(int x){
    heap_max[++heap_size_max]=x;
    int now=heap_size_max;
    while(now>>1){
        int father=now>>1;
        if(heap_max[father]<heap_max[now]) swap(heap_max[father],heap_max[now]);
        else break;
        now=father;
    }
}
void delete_heap_top_min(){
    heap_min[1]=heap_min[heap_size_min--];
    int now=1;
    while((now<<1)<=heap_size_min){
        int son=now<<1;
        if(son+1<=heap_size_min&&heap_min[son]>heap_min[son+1]) son++;
        if(heap_min[son]<heap_min[now]) swap(heap_min[son],heap_min[now]);
        else break;
        now=son;
    }
}
void delete_heap_top_max(){
    heap_max[1]=heap_max[heap_size_max--];
    int now=1;
    while((now<<1)<=heap_size_max){
        int son=now<<1;
        if(son+1<=heap_size_max&&heap_max[son]<heap_min[son+1]) son++;
        if(heap_max[son]>heap_max[now]) swap(heap_max[son],heap_max[now]);
        else break;
        now=son;
    }
}
signed main(){
    cin>>n;
    for(int i=1;i<=n;i++){cin>>a[i];}
    mid=a[1];
    cout<<mid<<endl;
    for(int i=2;i<=n;i++){
        if(a[i]>mid) push_min(a[i]);
        if(a[i]<=mid) push_max(a[i]);
        if(i&1){
            while(heap_size_min!=heap_size_max){
                if(heap_size_min<heap_size_max){
                    push_min(mid);
                    mid=heap_max[1];
                    delete_heap_top_max();
                }
                if(heap_size_min>heap_size_max){
                    push_max(mid);
                    mid=heap_min[1];
                    delete_heap_top_min();
                }
            }
            cout<<mid<<endl;
        }
    }
    return 0;
}

|