玄关,不输出,求改
  • 板块P1168 中位数
  • 楼主CCF___NOI
  • 当前回复10
  • 已保存回复10
  • 发布时间2024/8/14 20:40
  • 上次更新2024/8/14 23:39:42
查看原帖
玄关,不输出,求改
1394890
CCF___NOI楼主2024/8/14 20:40
#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;
}
  
2024/8/14 20:40
加载中...