还是板子题
因为旋律会同时加减一个数,所以我们在差分数组上做就好了
注意因为差分了,跨越的个数要少一个
基数排序循环写反了,调了好久
qwq
/*
@Date : 2019-07-19 10:17:22
@Author : Adscn (adscn@qq.com)
@Link : https://www.cnblogs.com/LLCSBlog
*/
#include<bits/stdc++.h>
using namespace std;
#define IL inline
#define RG register
#define gi getint()
#define gc getchar()
#define File(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
IL int getint()
{
RG int xi=0;
RG char ch=gc;
bool f=0;
while(ch<'0'|ch>'9')ch=='-'?f=1:f,ch=gc;
while(ch>='0'&ch<='9')xi=(xi<<1)+(xi<<3)+ch-48,ch=gc;
return f?-xi:xi;
}
template<typename T>
IL void pi(T k,char ch=0)
{
if(k<0)k=-k,putchar('-');
if(k>=10)pi(k/10,0);
putchar(k%10+'0');
if(ch)putchar(ch);
}
const int N=2e5+7;
int n;
int a[N];
namespace SA{
int rkx[N],rky[N],rank[N],sa[N],*key1=rkx,*key2=rky;
int height[N];
int cnt[N],m=380;
inline void init(){m=380;}
inline void Qsort()
{
memset(cnt,0,sizeof cnt);
for(int i=1;i<=n;++i)++cnt[key1[i]];
for(int i=1;i<=m;++i)cnt[i]+=cnt[i-1];
for(int i=n;i>=1;--i)sa[cnt[key1[key2[i]]]--]=key2[i];
}
inline void getsa()
{
key1=rkx,key2=rky;
for(int i=1;i<=n;++i)key1[i]=a[i],key2[i]=i;
Qsort();
for(int k=1,p=0;p<n;k<<=1,m=p)
{
p=0;
for(int i=n-k+1;i<=n;++i)key2[++p]=i;
for(int i=1;i<=n;++i)if(sa[i]>k)key2[++p]=sa[i]-k;
Qsort();
swap(key1,key2);
p=key1[sa[1]]=1;
for(int i=2;i<=n;++i)
key1[sa[i]]=(
key2[sa[i-1]]==key2[sa[i]]&&
key2[sa[i-1]+k]==key2[sa[i]+k])?p:++p;
}
for(int i=1;i<=n;++i)rank[sa[i]]=i;
}
inline void getheight()
{
for(int i=1,k=0;i<=n;++i)
{
if(k)--k;
int s=sa[rank[i]-1];
while(a[s+k]==a[i+k])++k;
height[rank[i]]=k;
}
}
}
using namespace SA;
int check(int mid)
{
int mx=sa[1],mn=sa[1];
for(int i=2;i<=n;++i)
if(height[i]<mid)mx=mn=sa[i];
else
{
mn=min(sa[i],mn),mx=max(sa[i],mx);
if(mx-mn>mid)return 1;
}
return 0;
}
int main(void)
{
while(~scanf("%d",&n))
{
if(n==0)break;
for(int i=1;i<=n;++i)a[i]=gi;
--n;
for(int i=1;i<=n;++i)a[i]=a[i+1]-a[i]+90;
init();
getsa();
getheight();
int l=0,r=n,ans;
while(l<=r)
{
int mid=(l+r)>>1;
if(check(mid))ans=mid,l=mid+1;
else r=mid-1;
}
printf("%d
",ans<4?0:ans+1);
return 0;
}
return 0;
}