思路:
二分一哈答案 height分个块
//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 100050
int n,s[N],cntA[N],cntB[N],A[N],B[N],tsa[N],sa[N],rk[N],ht[N];
void SA(){
for(int i=1;i<=n;i++)cntA[s[i]]++;
for(int i=1;i<=n;i++)cntA[i]+=cntA[i-1];
for(int i=n;i;i--)sa[cntA[s[i]]--]=i;
rk[sa[1]]=1;
for(int i=2;i<=n;i++)rk[sa[i]]=rk[sa[i-1]]+(s[sa[i]]!=s[sa[i-1]]);
for(int l=1;rk[sa[n]]<n;l<<=1){
memset(cntA,0,sizeof(cntA));
memset(cntB,0,sizeof(cntB));
for(int i=1;i<=n;i++)
cntA[A[i]=rk[i]]++,
cntB[B[i]=i+l<=n?rk[i+l]:0]++;
for(int i=1;i<=n;i++)cntA[i]+=cntA[i-1],cntB[i]+=cntB[i-1];
for(int i=n;i;i--)tsa[cntB[B[i]]--]=i;
for(int i=n;i;i--)sa[cntA[A[tsa[i]]]--]=tsa[i];
rk[sa[1]]=1;
for(int i=2;i<=n;i++)rk[sa[i]]=rk[sa[i-1]]+(A[sa[i]]!=A[sa[i-1]]||B[sa[i]]!=B[sa[i-1]]);
}
for(int i=1,j=0;i<=n;i++){
j=j?j-1:0;
while(s[i+j]==s[sa[rk[i]-1]+j])j++;
ht[rk[i]]=j;
}
}
bool check(int x){
int maxx,minn=100050;
for(int i=1;i<=n;i++){
if(ht[i]<x)maxx=minn=sa[i];
else{
maxx=max(maxx,sa[i]);
minn=min(minn,sa[i]);
if(maxx-minn>=x)return 1;
}
}return 0;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&s[i]);
}
SA();
int l=0,r=n/2,ans;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid))ans=mid,l=mid+1;
else r=mid-1;
}
printf("%d\n",ans);
}