分治
#include<bits/stdc++.h> using namespace std; const int N=1e6; #define ll long long long long tmin[N],tmax[N],h[N],ans; void mk(int o,int l,int r){ if(l==r){ tmin[o]=tmax[o]=l; }else{ int mid=(r+l)/2; mk(o*2,l,mid); mk(o*2+1,mid+1,r); ll t1,t2; t1=tmin[o*2]; t2=tmin[o*2+1]; if(h[t1]>=h[t2])tmin[o]=t2; else tmin[o]=t1; t1=tmax[o*2]; t2=tmax[o*2+1]; if(h[t1]>=h[t2])tmax[o]=t1; else tmax[o]=t2; } } ll ppmin,ppmax,ql,qr; void qz(int o,int l,int r){ if(ql<=l&&r<=qr){ int dot=tmax[o]; if(h[dot]>h[ppmax])ppmax=dot; dot=tmin[o]; if(h[dot]<=h[ppmin])ppmin=dot; }else{ int m=(r+l)/2; if(ql<=m)qz(o*2,l,m); if(qr>m)qz(o*2+1,m+1,r);} } ll n; void work(int l,int r){ if(l>=r)return; if(l<=0)return; if(r<=0)return; ppmin=200000; ppmax=0; ql=l;qr=r; qz(1,1,n); ll pmax=ppmax; ll pmin=ppmin; if(pmin<pmax){ work(l,pmin-1); work(pmax+1,r); ans=max(ans,pmax-pmin+1); return; }else{ work(l,pmax); work(pmax+1,pmin-1); work(pmin,r); return; } } int main(){ h[200000]=10000000000000; ppmin=200000; ios::sync_with_stdio(0); //freopen("p.in","r",stdin); //freopen("p.out","w",stdout); cin>>n; for(int i=1;i<=n;i++) cin>>h[i]; mk(1,1,n); work(1,n); cout<<ans; return 0; }