求助,本地AC,提交RE
  • 板块P6510 奶牛排队
  • 楼主00_00
  • 当前回复1
  • 已保存回复1
  • 发布时间2021/11/6 21:47
  • 上次更新2023/11/4 01:13:34
查看原帖
求助,本地AC,提交RE
405253
00_00楼主2021/11/6 21:47
#include <bits/stdc++.h>
using namespace std;

const int N=110001,LogN=19;

int logn[N],a[N],b[N][LogN],c[N][LogN],bb[N][LogN],cc[N][LogN];
int n,ans=0;

int maxx(int x,int y)
{
    if(b[x][y-1]<b[x+(1<<y-1)][y-1])
    {
        b[x][y]=b[x+(1<<y-1)][y-1];
        bb[x][y]=bb[x+(1<<y-1)][y-1];
    }
    else
    {
        b[x][y]=b[x][y-1];
        bb[x][y]=bb[x][y-1];
    }
}

int minn(int x,int y)
{
    if(c[x][y-1]<c[x+(1<<y-1)][y-1])
    {
        c[x][y]=c[x][y-1];
        cc[x][y]=cc[x][y-1];
    }
    else
    {
        c[x][y]=c[x+(1<<y-1)][y-1];
        cc[x][y]=cc[x+(1<<y-1)][y-1];
    }
}

int pd(int x,int y,int z)
{
    int s=logn[y-x+1];
    if(z==1)
    {
        if(b[x][s]<b[y-(1<<s)+1][s]) return bb[y-(1<<s)+1][s];
        else return bb[x][s];
    }
    else
    {
        if(c[x][s]>c[y-(1<<s)+1][s]) return cc[y-(1<<s)+1][s];
        else return cc[x][s];
    }
}

inline void solve(int x,int y)
{
    int l=pd(x,y,2),r=pd(x,y,1);
    if(l==r) return;
    if(l<=r)
    {
        ans=max(r-l+1,ans);
        if(l-x>ans) solve(x,l-1);
        if(y-r>ans) solve(r+1,y);
    }
    else
    {
        if(r-x+1>ans) solve(x,r);
        if(l-1-r>ans) solve(r+1,l-1);
        if(y-l+1>ans) solve(l,y);
    }
}

inline int get()
{
    char ch; int res=0,f=0;
    while(ch=getchar(),(ch<'0' || ch>'9'));
    res=ch-'0';
    while(ch=getchar(),ch>='0' && ch<='9') res=res*10+ch-'0';
    return res;
}

int main()
{
    n=get();
    for(int i=1;i<=n;++i) a[i]=get();
    logn[0]=-1;
    for(int i=1;i<=n;++i)
        logn[i]=logn[i>>1]+1,b[i][0]=c[i][0]=a[i],bb[i][0]=cc[i][0]=i;
    for(int j=1;j<LogN;++j)
    for(int i=1;i+(1<<j)-1<=n;++i)
    {
        maxx(i,j); minn(i,j);
    }
    solve(1,n);
    printf("%d",ans);
}
2021/11/6 21:47
加载中...