Non-negative parts and

Source: Internet
Author: User
Tags bool data structures integer

Description

Xiao Mu classmate recently received a task: give a sequence containing n integers: a0,a1,..., An-1, after moving K-bit, the sequence becomes ak,ak+1,..., an-1,a0,a1,..., Ak-1. An excellent move is: for any of the preceding I (1<=i<=n) entries and both satisfy no less than 0. Please calculate the number of excellent cyclic movements for this sequence.
But small mu classmate is still small, can not solve this problem, please you help him.

Input

The first line is an integer n, which indicates the number of N.
The second row n integers, the Ai represents the number of numbers given.

Output

An integer that represents the number of excellent loop moves.

Sample Input

Input Sample 1:
3
2 2 3
Input Sample 2:

5 3-1 2-3
4

Sample Output

Output Example 1:
3
Output Example 2:
2

Hint

"Input and Output sample description"
Sequence of 3 elements: 2 2 3, the loop movement is as follows:
The loop moves 0-bit to get the sequence: 2 2 3, the first I and respectively: 2 4 7, meet the conditions;
The loop moves 1-bit to get the sequence: 2 3 2, the first I and respectively: 2 5 7;
The loop moves 2-bit to get the sequence: 3 2 2, the first I and respectively: 3 5 7;
So the number of excellent circular moves is 3.
"Data Range"
30% of the data meet: 1<=n<=5,000
50% of the data meet: 1<=n<=10,000
100% of the data meet: 1<=n<=1,000,000,-1,000<=ai<=1,000


Analysis

Official :

Copy the given sequence of two parts:

A0, A1 、...、 An-1, an, an+1 、...、 an+n-1 (where An+i=ai i=0,1,2,..., n-1)

Then the sequence of the cyclic K-bit is the above sequence: Ak,ak+1,..., ak+n-1.

If the Sum[i]=a0+a1+...+ai is set, the same as the preceding P term of the cyclic K-bit sequence is: sum[k+p]-sum[k-1];

It can be concluded that: judging the ring moving K-bit after the sequence of any preceding P-term and is not less than 0, only judge the minimum sum[k+p] and sum[k-1] The difference is greater than or equal to 0.

For the smallest sum[k+p], you can use one of the following data structures to optimize:

1, line tree: Time complexity O (2nlog2n);

2, Priority queue: Time complexity O (2nlog2n);

3, monotone queue: Time complexity O (2n); a wonderful way to do this:

First, copy the sequence again. In this case, we discuss in reverse order I, if v[i] is negative, then the position of I is not as good as the beginning of the sequence, marked true. (The mappings in 1~n are also marked as true). Then with a j=i-1, to the previous discussion (if v[i]>=0 without J discussion), if sum[i]-sum[j-1]<0, obviously this paragraph can not start as a good sequence, Mark J for True,j--。 However, if there is sum[i]-sum[j-1]>=0, then break the current J layer loop, i=j;

There is a question, if the distance of j~i is bigger than N. Will not be marked wrong. No. Because Sum[i]-sum[i-n] is a fixed value, pay attention to the above break. Must have ruled out this situation. Finally sweep the tag array again, the number of false is the answer. The time complexity is O (N);


Code

/* Code for the wonderful method */#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include &l
t;ctime> #include <iostream> #include <algorithm> using namespace std;   int n,v[2000005],sum[2000005];        Original arrays and prefixes and array bool ans[1000005];
    Tag array void _in (int &x,bool mark=false)//input is too large to optimize {char t; for (; T=getchar (),t< ' 0 ' | |
    T> ' 9 ';) if (t== '-') mark=1; for (x=t-' 0 ', T=getchar (); '
    0 ' <=t&&t<= ' 9 '; x=x*10+t-' 0 ', T=getchar ());
x=mark?-x:x;
	} void _init () {_in (N);
		for (int i=1;i<=n;i++) {_in (v[i]);     V[i+n]=v[i];
Copy sequence} for (int i=1;i<= (n<<1); i++)//COMPUTE prefix and sum[i]=sum[i-1]+v[i];     } void _solve () {for (int i= (n<<1); i>=n+1;i--)//reverse-order Discussion {if (v[i]>=0) continue;     If V[i] is not a negative number, it is not discussed (avoid repeating the discussion) Ans[i-n]=true;
		Mark Int J;     for (j=i-1;j>=1;j--) {if (sum[i]-sum[j-1]>=0) break; 
			The essential...     else {int t= (j>n)? J-n:j; 
Judge whether J is in 1~n or n+1~2*n			    Ans[t]=true;     }} i=j;
	If not added, timeout} int tot=0;
	for (int i=1;i<=n;i++) if (!ans[i]) tot++;
printf ("%d\n", tot);
	} int main () {_init ();
	_solve ();
return 0; }


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.