比赛 20240913练习 评测结果 AAAAAAAATA
题目名称 奶牛排队 最终得分 90
用户昵称 flyfree 运行时间 2.402 s
代码语言 C++ 内存使用 6.57 MiB
提交时间 2024-09-13 21:03:36
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 100010
inline ll read(){
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar(); 
    }
    return x*f;
}
ll n,idx,ansz;
ll lsh[MAXN];
ll a[MAXN],s1[MAXN],s2[MAXN],s_up[MAXN],s_down[MAXN];
bool cmp(ll a,ll b){
    return a>b;
}
ll lowbit(ll now){
    return now&(-now);
} 
ll find(ll x){
    ll l=1,r=idx;
    while(l<r){
        ll mid=(l+r)/2;
        if(lsh[mid]<x)l=mid+1;
        else r=mid;
    }
    return l;
}
void ad_up(ll now,ll s){
    while(now>0){
        s1[now]=s;
        now-=lowbit(now);
    }
}
ll re_up(ll now){
    ll ans=0;
    while(now<=idx){
        ans=max(ans,s1[now]);
        now+=lowbit(now);
    }
    return ans;
}
void ad_down(ll now,ll s){
    while(now<=idx){
        s2[now]=s;
        now+=lowbit(now);
    }
}
ll re_down(ll now){
    ll ans=n+1;
    while(now>0){
        ans=min(ans,s2[now]);
        now-=lowbit(now);
    }
    return ans;
}
int main(){
    freopen("tahort.in","r",stdin);
    freopen("tahort.out","w",stdout);
    n=read();
    for(int i=1;i<=n;i++){
        a[i]=read();
        lsh[++idx]=a[i];
    }
    a[0]=LONG_LONG_MAX;
    lsh[++idx]=a[0];
    sort(lsh+1,lsh+2+n);
    for(int i=1;i<=n+1;i++){
        if(lsh[i]==lsh[i+1])lsh[i]=0,idx--;
    }
    sort(lsh+1,lsh+2+n,cmp);
    sort(lsh+1,lsh+1+idx);
    for(int i=0;i<=n;i++){
        a[i]=find(a[i]);
        if(i){
            s_up[i]=re_up(a[i]);
//            cout<<s_up[i]<<" ";
        }
//        cout<<a[i]<<endl;
        ad_up(a[i],i);
    }
//    cout<<endl;
    for(int i=0;i<=idx;i++)s2[i]=n+1;
    for(int i=n;i>=1;i--){
        s_down[i]=re_down(a[i]);
//        cout<<s_down[i]<<" ";
        ad_down(a[i],i); 
    }
    s_up[n+1]=n;
    for(int i=1;i<=n;i++){
        ll r=s_down[i];
//        cout<<endl<<i<<" ";
        while(s_up[r]>=i&&r>i){
//            cout<<r<<" ";
           r=s_up[r];
        }
//        cout<<endl;
        if(r==i)continue;
        ansz=max(ansz,r-i+1);
    }
    cout<<ansz;
    return 0;
}