Test Instructions:
Professor L is currently working on a project on the human genome that can be thought of as a sequence of n-length: a0,a1,......, An-1. After the gene sequence moves k-bit, a new gene sequence can be obtained: ak,ak+1,..., an-1,a0,a1,..., Ak-1. When a gene sequence satisfies an arbitrary pre-I (1<=i<=n) term and satisfies no less than 0, we call this gene sequence a high-quality gene sequence.
Since Professor L has been busy recently, I've found you in the lab, and your task is to help Professor L count the number of good gene sequences. Ideas:
Copy the given sequence two parts:
a[0], a[1] 、...、 a[n-1], a[n], a[n+1] 、...、 a[n+n-1] (where a[n+i]=a[i],i=0,1,2,..., n-1)
Then the sequence of the cyclic K-bits is the sequence of the above: A[k],a[k+1],..., a[k+n-1].
Sum[i]=a[0]+a[1]+...+a[i], the sum of any of the preceding P-entries of the cyclic K-bit sequence is: sum[k+p]-sum[k-1];
It can be concluded that the sequence of the K-bit after the ring moves is no less than 0, and only the smallest sum is judged. The difference between [k+p] and sum[k-1] is greater than or equal to 0.
As for finding the smallest sum[k+p], the monotone queue can be used: Time complexity O (2n);
Program:
const maxn=10000000; var n,i,ans:longint;
F,g,v,s:array[0..maxn]of Longint; Begin assign (input, ' genes.in ');
Reset (input); Assign (output, ' genes.out ');
Rewrite (output);
READLN (n);
For I:=1 to n do begin read (V[i]);
S[i]:=s[i-1]+v[i];
End
F[1]:=1;
G[n]:=n;
For i:=2 to N does if s[i]<s[f[i-1]] then f[i]:=i else f[i]:=f[i-1];
For i:=n-1 Downto 1 does if s[i]<s[g[i+1]] then g[i]:=i else g[i]:=g[i+1];
If S[f[n]]>=0 then Inc (ANS);
For i:=2 to N does if (s[g[i]]-s[i-1]>=0) and (S[f[i-1]]+s[n]-s[i-1]>=0) then Inc (ANS);
Writeln (ANS); Close (input);
Close (output); End.