Problem Description
You are given a sequence of n numbers A0,..., an-1. A cyclic shift by K positions (0<=k<=n-1) results in the following Sequence:ak ak+1,..., an-1, A0, A1,..., Ak-1. How many of the n cyclic shifts satisfy the condition the sum of the first I numbers are greater than or equal to zero For any I with 1<=i<=n?
Input
Each test case consists of lines. The first contains the number n (1<=n<=106), the number of integers in the sequence. The second contains n integers a0,..., an-1 ( -1000<=ai<=1000) representing the sequence of numbers. The input would finish with a line containing 0.
Output
For each test case, print one line with the number of cyclic shifts of the given sequence which satisfy the condition stat Ed above.
Sample Input
3
2 2 1
3
-1 1 1
1
-1
0
Sample Output
3
2
0
Source
SWERC 2011
Recommend
LCY | We have carefully selected several similar problems for you:4190 4192 4187 4188 4189
My first reaction to this problem is that it can be converted to sliding window problems, and then the monotone queue solves
Here I'm using a monotonous stack of practices, the sequence is expanded one time, then from right to left to record the suffix and, and then start to maintain an incremental monotonic stack, if the top of the stack is less than the sum of the current stack, it is removed, assuming that the top position of the stack is I, now to the stack position is J, then the description interval [i, j-1] value is negative So the sequence starting with the first element is illegal.
The last statistic is legal.
Complexity O (N)
/************************************************************************* > File Name:hdu4193.cpp > Auth Or:alex > Mail: [email protected] > Created time:2015 May 11 Monday 14:13 22 seconds ******************************** ****************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <list>#include <bitset>#include <set>#include <vector>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;Static Const intN =1100100;intArr[n <<1];intSum[n <<1]; Stack <PLL>Stack;BOOLFlag[n <<1];intMain () {intN while(~scanf("%d", &n), n) {sum[2* N] =0; for(inti =1; I <= N; ++i) {scanf("%d", &arr[i]); Flag[i] =1; } for(inti = n +1; I <=2N1; ++i) {Arr[i] = arr[i-n]; } for(inti =2N1; I >=1; -i) {Sum[i] = sum[i +1] + arr[i]; }//Top = 0; while(! Stack.empty ()) {Stack.pop (); } for(inti =1; I <=2N1; ++i) {if(Stack.empty ()) {Stack.push (Make_pair (sum[i], i)); }Else{ while(! Stack.empty ()) {PLL u = stack.top ();if(U.first >= Sum[i]) { Break; } stack.pop ();intlen = I-u.second;if(Len <= N) {Flag[u.second] =0; }} stack.push (Make_pair (sum[i], i)); } } while(! Stack.empty ()) {PLL u = stack.top (); Stack.pop ();if(U.first <0&& u.second <= N) {Flag[u.second] =0; } }intAns =0; for(inti =1; I <= N; ++i) {ans + = flag[i]; }printf("%d\n", ans); }return 0;}
hdu4193---non-negative Partial Sums (monotonic queue or monotonic stack)