标签:tail fail cst scanf play assert print algo div
1 #include<cstring> 2 #include<cstdlib> 3 #include<cstdio> 4 //#include<assert.h> 5 //#include<time.h> 6 #include<math.h> 7 //#include<queue> 8 #include<algorithm> 9 //#include<iostream> 10 using namespace std; 11 12 int n; 13 #define maxn 100011 14 int a[maxn],fail[maxn],indo[maxn]; 15 int ll[maxn],rr[maxn]; 16 int da[maxn],xiao[maxn],pos[maxn]; 17 void pre() 18 { 19 for (int i=0;i<=n;i++) rr[i]=i+1; 20 for (int i=1;i<=n+1;i++) ll[i]=i-1; 21 for (int i=n;i>=1;i--) 22 { 23 da[i]=pos[rr[a[i]]]; 24 xiao[i]=pos[ll[a[i]]]; 25 rr[ll[a[i]]]=rr[a[i]]; 26 ll[rr[a[i]]]=ll[a[i]]; 27 cout<<da[i]<<‘ ‘<<xiao[i]<<endl; 28 } 29 } 30 void makefail(int x) 31 { 32 if (x<3) fail[x]=1; 33 else 34 { 35 int j=fail[x-1]; 36 while (j>1 && !((!da[j] || a[x-1-(j-da[j])]>a[x-1]) && (!xiao[j] || a[x-1-(j-xiao[j])]<a[x-1]))) j=fail[j]; 37 fail[x]=((!da[j] || a[x-1-(j-da[j])]>a[x-1]) && (!xiao[j] || a[x-1-(j-xiao[j])]<a[x-1]))?j+1:1; 38 indo[fail[x]]++; 39 // cout<<x<<‘ ‘<<fail[x]<<endl; 40 } 41 } 42 int que[maxn],head,tail,size[maxn]; 43 #define LL long long 44 LL work() 45 { 46 head=tail=0; 47 for (int i=2;i<=n+1;i++) 48 { 49 if (!indo[i]) que[tail++]=i; 50 size[i]=1; 51 } 52 LL ans=0; 53 while (head!=tail) 54 { 55 const int now=que[head++]; 56 if (now==1) continue; 57 indo[fail[now]]--; 58 size[fail[now]]+=size[now]; 59 if (!indo[fail[now]]) que[tail++]=fail[now]; 60 ans=max(ans,(now-1)*1ll*size[now]); 61 } 62 return ans; 63 } 64 int main() 65 { 66 // freopen("sub.in","r",stdin); 67 // freopen("sub.out","w",stdout); 68 scanf("%d",&n); 69 for (int i=1;i<=n;i++) scanf("%d",&a[i]),pos[a[i]]=i; 70 pre(); 71 fail[1]=1; 72 for (int i=1;i<=n;i++) makefail(i+1); 73 printf("%lld\n",work()); 74 return 0; 75 }
标签:tail fail cst scanf play assert print algo div
原文地址:http://www.cnblogs.com/Blue233333/p/7763051.html