玄关,不输出,求改

P1168 中位数

CCF___NOI @ 2024-08-14 20:40:20

#include<bits/stdc++.h>
using namespace std;
int h[5000001],h1[1000001],len1,b,c,n,m,s,x,t,u,v,len;
void up(int x){
    while(x!=1&&h[x]<h[x/2])
    {
        swap(h[x],h[x/2]);
        x/=2;
    }
}
void down(int x){
    while(h[x]>h[2*x]&&2*x<=len||h[x]>h[2*x+1]&&2*x+1<=len)
    {
        if(2*x+1>len)
        {
            swap(h[x],h[2*x]);
            x*=2;
        }
        else
        {
            if(h[x*2]<h[x*2+1])
            {
                swap(h[x],h[2*x]);
                x*=2;
            }
            else
            {
                swap(h[x],h[x*2+1]);
                x=2*x+1;
            } 
        }
    }
}

void up1(int x){
    while(x!=1&&h[x]>h[x/2])
    {
        swap(h[x],h[x/2]);
        x/=2;
    }
}
void down1(int x){
    while(h1[x]<h1[2*x]&&2*x>=len||h1[x]>h1[2*x+1]&&2*x+1>=len)
    {
        if(2*x+1<len)
        {
            swap(h1[x],h1[2*x]);
            x*=2;
        }
        else
        {
            if(h1[x*2]>h1[x*2+1])
            {
                swap(h1[x],h1[2*x]);
                x*=2;
            }
            else
            {
                swap(h1[x],h1[x*2+1]);
                x=2*x+1;
            } 
        }
    }
}
int main(){
    cin>>n;
    for(int i=1,op,x;i<=n;i++){
        cin>>x;
        len1++;
        h1[len1]=x;
        up1(len1);
        if(i%2==1)
        {
            while(len1&&len&&h1[1]>h[1])
           {
               len++;
               h[len]=h1[i];
               up(len);

               swap(h1[1],h1[len1]);
               len1--;
               down1(1);
           }
           while(len1>len)
           {
               len++;
               h[len]=h1[i];
               up(len);

               swap(h1[1],h1[len1]);
               len1--;
               down1(1);
           }
           while(len1>len-1)
           {
               len++;
               h[len]=h1[i];
               up(len);

               swap(h1[1],h1[len1]);
               len1--;
               down1(1);
           }
                cout<<h[1]<<endl;
        }
    }
    return 0;
}

by guanzisheng2 @ 2024-08-15 07:55:13

#include<bits/stdc++.h>
using namespace std;
int n,a[2000005],len;
void up(int x){//小根堆 
    while(a[x]<a[x/2]&&x!=1){
        swap(a[x],a[x/2]);
        x/=2;
    }
}
void down(int x){
    while((2*x<=len&&a[x]>a[x*2])||(a[x]>a[2*x+1]&&2*x+1<=len)){
        if(2*x+1>len){
            swap(a[x],a[2*x]);
            x=x*2;
        }
        else{
            if(a[2*x]<a[2*x+1]){
                swap(a[x],a[2*x]);
                x=x*2;
            }
            else{
                swap(a[x],a[2*x+1]);
                x=x*2+1;
            }
        }
    }
}
int a1[2000005],len1;
void up1(int x){//大根堆 
    while(a1[x]>a1[x/2]&&x!=1){
        swap(a1[x],a1[x/2]);
        x/=2;
    }
}
void down1(int x){
    while((2*x<=len1&&a1[x]<a1[x*2])||(a1[x]<a1[2*x+1]&&2*x+1<=len1)){
        if(2*x+1>len1){
            swap(a1[x],a1[2*x]);
            x=x*2;
        }
        else{
            if(a1[2*x]>a1[2*x+1]){
                swap(a1[x],a1[2*x]);
                x=x*2;
            }
            else{
                swap(a1[x],a1[2*x+1]);
                x=x*2+1;
            }
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(int i=1,x;i<=n;i++){
        cin>>x;
        len1++;
        a1[len1]=x;
        up1(len1);
        if(i%2==1){
            while(len1&&len&&a1[1]>a[1]){
                len++;
                a[len]=a1[1];
                up(len);
                swap(a1[1],a1[len1]);
                len1--;
                down1(1);
            }
            while(len1>=len){
                len++;
                a[len]=a1[1];
                up(len);
                swap(a1[1],a1[len1]);
                len1--;
                down1(1);
            }
            while(len1<len-1){
                len1++;
                a1[len1]=a[1];
                up1(len1);
                swap(a[1],a[len]);
                len--;
                down(1);
            }
            cout<<a[1]<<"\n"; 
        }
    }
}

by niuqichongtian @ 2024-11-16 00:54:12

@CCF___NOI

意思就是 题目让输出

\displaystyle ⌊ \frac {N+1} {2} ⌋


by niuqichongtian @ 2024-11-16 01:26:17

@CCF___NOI

你这代码死循环了吧。如果死循环建议查查有没有可能一直向上更新,到堆顶了还没有停下来的情况。

当然我对也不是很了解,您可以参考着我说的来


上一页 |