标签:this sample range auto about center val false hint
Time Limit: 1000MS | Memory Limit: 30000K | |
Description
Input
Output
Sample Input
30 25 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 18 82 78 74 70 66 67 64 60 65 80 0
Sample Output
5
Hint
Source
#include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #define N 20010 using namespace std; int n,p,q=1,k,a[N],rk[2][N],sa[2][N],v[N],h[N],ans=-1,f[N][16]; char s[N]; void mul(int *sa,int *rk,int *SA,int *RK) { for(int i=1;i<=n;i++) v[rk[sa[i]]]=i; for(int i=n;i;i--) if(sa[i]>k) SA[v[rk[sa[i]-k]]--]=sa[i]-k; for(int i=n-k+1;i<=n;i++) SA[v[rk[i]]--]=i; for(int i=1;i<=n;i++) RK[SA[i]]=RK[SA[i-1]]+(rk[SA[i]]!=rk[SA[i-1]]||rk[SA[i]+k]!=rk[SA[i-1]+k]); } void presa() { for(int i=1;i<=n;i++) v[a[i]]++; for(int i=1;i<=180;i++) v[i]+=v[i-1]; for(int i=1;i<=n;i++) sa[p][v[a[i]]--]=i; for(int i=1;i<=n;i++) rk[p][sa[p][i]]=rk[p][sa[p][i-1]]+(a[sa[p][i]]!=a[sa[p][i-1]]); for(k=1;k<n;k<<=1,swap(p,q)) { mul(sa[p],rk[p],sa[q],rk[q]); if(rk[q][sa[q][n]]==n) { swap(p,q); return; } } } void getheight() { int j,g=0; for(int i=1;i<=n;i++) { j=sa[p][rk[p][i]-1]; while(a[i+g]==a[j+g]) g++; h[rk[p][i]]=g;if(g) g--; } } void pre() { p=0;q=1;k=0; memset(a,0,sizeof(a)); memset(rk,0,sizeof(rk)); memset(sa,0,sizeof(sa)); memset(v,0,sizeof(v)); memset(h,0,sizeof(h)); ans=-1; } bool check(int mid) { int minn=n+1,maxn=-1; for(int i=2;i<=n;i++) { if(h[i]<mid) { if(maxn-minn>=mid) return true; minn=n+1;maxn=-1; } minn=min(minn,sa[p][i]); maxn=max(maxn,sa[p][i]); } if(maxn-minn>=mid) return true; return false; } void solve() { int l=4,r=n,mid; while(l<=r) { mid=l+r>>1; if(check(mid)) ans=mid,l=mid+1; else r=mid-1; } printf("%d\n",ans+1); } int main() { while(scanf("%d",&n)) { if(!n) return 0; pre(); int last,x; scanf("%d",&last); for(int i=2;i<=n;i++) { scanf("%d",&x); a[i-1]=x-last; a[i-1]+=88; last=x; } n--; presa(); getheight(); solve(); } }
标签:this sample range auto about center val false hint
原文地址:http://www.cnblogs.com/TheRoadToTheGold/p/6594317.html