Bzoj 2119: Stock market forecasts [suffix array st table]

Source: Internet
Author: User
Tags pow

2119: Stock Market Forecast Time limit:10 Sec Memory limit:259 MB
submit:331 solved:153
[Submit] [Status] [Discuss] Description

Ink's mother Love stocks, she asked ink for her to write a software to predict the future trend of a stock. Stock Line chart is a necessary tool to study stock, it shows the trend of stock clearly through a function image of time and stock price. After a long period of observation, the ink found that many stocks have the following rules: the previous trend is likely to be reproduced in a short period of time! You can see that the share price of part A is the same as the share price of part C. Through this observation, the ink thinks he may have found a way to predict the future trend of stocks. Further research but puzzled ink, he would like to try to count the length of part B and the probability of the occurrence of the relationship, but because of the large amount of data, relying on the power of the human brain is difficult to complete, so ink found good at programming you, please help him find a given recurrence of the interval (b part of the length), How many time periods meet the first and the end of the trend is exactly the same? Of course, the length of the end part cannot be zero.

Input

The first line of input contains two integers n, M, each representing the total time to be counted and the intervals to reproduce (the length of part B). The next n lines, one integer per line, represent the share price at each point in time.

Output

Outputs an integer that represents the number of time periods that satisfy a condition

Sample Input12 4
1 2 3 4 8 9 1 2 3 4 8 9
Sample Output6
"Sample description"
6 time periods are: 3-9, 2-10, 2-8, 1-9, 3-11, 4-12.
HINT

For 100% of data, all occurrences of 4≤n≤50000 1≤m≤10 m≤n have no more than 32-bit signed integers.

The same trend, again using the differential

and to be discretized.

And then just ask how many substrings are ABA

Then, similar to the idea on the question, enumerate the same length L, set one key point per l

found that for an ABA, the first a must cover and cover only one key point ,

Enumeration key points I,i and I+b+l to the left to the right to match the length L and R (again, my R includes the key points), if L+r>=l then this place has l+r-l+1 a satisfied substring

(i)--l--b--(i+b+l)--l--

In order to do not repeat, the left and right extend the maximum length of L-1

Why don't you repeat that? I this idiot unexpectedly for a long time, because the above blue font, each key point extends out, the first L must cover this key point, from other key points do not cover, so do not repeat not missing

Attention:

1. The counting sort is finally reversed

2. Because my r includes key points, so the maximum is L not L-1

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intn=5e4+5, inf=1e9;inlineintRead () {CharC=getchar ();intx=0, f=1;  while(c<'0'|| C>'9'){if(c=='-') f=-1; c=GetChar ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;}intB,s[n],mp[n],tot;intN,m,c[n],t1[n],t2[n];intBin (intv) {    intL=1, r=tot;  while(l<=R) {        intMid= (l+r) >>1; if(MP[MID]==V)returnmid; Else if(V<mp[mid]) r=mid-1; ElseL=mid+1; }    return 0;} InlineBOOLcmpint*r,intAintBintj) {    returna+j<=n&&b+j<=n&&r[a]==r[b]&&r[a+j]==r[b+j];}intlog[n],pow[ -],mn[n][ -];voidinist () {pow[0]=1; for(intI=1;i< -; i++) pow[i]=pow[i-1]<<1; log[0]=-1; for(intI=1; i<=n;i++) log[i]=log[i/2]+1;}voidGETST (intmn[n][ -],inta[]) {     for(intI=1; i<=n;i++) mn[i][0]=A[i];  for(intj=1; j<=log[n];j++)         for(intI=1; i+pow[j]-1<=n;i++) Mn[i][j]=min (mn[i][j-1],mn[i+pow[j-1]][j-1]);}structsa{intsa[n],rnk[n],height[n],mn[n][ -]; voidgetheight () {intk=0;  for(intI=1; i<=n;i++) rnk[sa[i]]=i;  for(intI=1; i<=n;i++){            if(k) k--; if(rnk[i]==1)Continue; intj=sa[rnk[i]-1];  while(I+k<=n&&j+k<=n&&s[i+k]==s[j+k]) k++; Height[rnk[i]]=K; }    }    voidGetsa () {int*r=t1,*k=T2;  for(intI=0; i<=m;i++) c[i]=0;  for(intI=1; i<=n;i++) c[r[i]=s[i]]++;  for(intI=1; i<=m;i++) c[i]+=c[i-1];  for(inti=n;i>=1; i--) sa[c[r[i]]--]=i;  for(intj=1; j<=n;j<<=1){            intp=0;  for(inti=n-j+1; i<=n;i++) k[++p]=i;  for(intI=1; i<=n;i++)if(SA[I]&GT;J) k[++p]=sa[i]-J;  for(intI=0; i<=m;i++) c[i]=0;  for(intI=1; i<=n;i++) c[r[k[i]]]++;  for(intI=1; i<=m;i++) c[i]+=c[i-1];  for(inti=n;i>=1; i--) sa[c[r[k[i]]]--]=K[i]; Swap (r,k);p=0; r[sa[1]]=++p;  for(intI=2; i<=n;i++) r[sa[i]]=cmp (k,sa[i],sa[i-1],J)? p:++p; if(p>=n) Break; m=p; }    }        intLcpintXintY) {//printf ("LCP%d%d\n", x, y);x=rnk[x];y=Rnk[y]; if(x>y) Swap (x, y); x + +;//printf ("Rnk%d%d\n", x, y);        intt=log[y-x+1];//printf ("T%d%d%d\n", t,mn[x][t],mn[y-pow[t]+1][t]);        returnMin (mn[x][t],mn[y-pow[t]+1][t]); }        voidIni () {Getsa (); GetHeight (); getst (mn,height); }    voidTest () {puts ("Test");  for(intI=1; i<=n;i++) printf ("#df", S[i]);p UTS ("");  for(intI=1; i<=n;i++) printf ("%d", Rnk[i]);p UTS ("");  for(intI=1; i<=n;i++) printf ("%d", Sa[i]);p UTS ("");  for(intI=1; i<=n;i++) printf ("%d", Height[i]);p UTS (""); Puts (""); }}a,b;intans;voidSolveintL) {//printf ("Sol%d\n", L);     for(intI=1; i+b+l<=n;i+=L)if(S[i]==s[i+b+l]) {//puts ("Begin");            intR=A.LCP (I,i+b+l), L=B.LCP (n-i+2, n-i-b-l+2); L=min (l,l-1); r=min (r,l); //printf ("ss%d%d%d%d\n", i,i+b+l,l,r);            if(l+r>=l) ans+=l+r+1-L;//, printf ("Hi%d%d\n", i,i+b+l); //puts ("end");        }}intMain () {//freopen ("In.txt", "R", stdin);N=read (); b=read ();  for(intI=1; i<=n;i++) {S[i]=read (); if(i!=1) mp[++tot]=s[i-1]=s[i]-s[i-1]; } N--; Sort (MP+1, mp+1+N); Tot=0; mp[++tot]=mp[1];  for(intI=2; i<=n;i++)if(mp[i]!=mp[i-1]) mp[++tot]=Mp[i];  for(intI=1; i<=n;i++) s[i]=Bin (S[i]); M=N;    Inist (); A.ini ();//a.test ();Reverse (s+1, s+1+N); B.ini ();//b.test ();Reverse (s+1, s+1+N);  for(intL=1; l+l+b<=n; l++) solve (L); printf ("%d", ans);}

Bzoj 2119: Stock market forecasts [suffix array st table]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.