Problem descriptionyou is 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 fi rst I numbers are greater than or equal to Zer O for any I with 1<=i<=n?
Inputeach test case consists of the lines. The fi rst 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.
Outputfor each test case, print one line with the number of cyclic shifts of the given sequence which the satisfy N stated above.
Sample Input
32 2 13-1) 1 11-10
Sample Output
320
It's only a little bit more about the monotone queue.
Test instructions: A sequence, each time the first number is put to the tail, to determine whether the sequence for any I (1<=i<=n) is satisfied with the sum[i]>=0, if the ans++ to meet the maximum ans
Idea: Double the string first, the queue needs to maintain the minimum value of the sequence of length n in the first team
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <queue> #include <stack> #include <vector> #include <set> #include <map > #define L (x) (x<<1) #define R (x) (x<<1|1) #define MID (x, y) ((x+y) >>1) #define EPS 1e-8typedef __ Int64 ll; #define FRE (i,a,b) for (i = A; I < b; i++) #define FREE (i,a,b) for (i = A; i > =b;i--) #define MEM (T, v) mem Set ((t), V, sizeof (t)) #define SSF (n) scanf ("%s", N) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF Printf#define bu G PF ("hi\n") using namespace std, #define INF 0x3f3f3f3f#define n 2000005int sum[n],a[n],n;int que[n],tail,head;void Inque (int i) {while (Head<tail&&sum[i]<sum[que[tail-1]]) tail--;que[tail++]=i;} void Outque (int i) {if (I-n>=que[head]) head++;} int main () {int i,j;while (~SF (n), N) {fRe (i,1,n+1) {SF (a[i]); Sum[i]=sum[i-1]+a[i]; } fre (i,n+1,n*2+1) sum[i]=sum[i-1]+a[i-n]; Tail=head=0; int ans=0; Fre (i,1,n) inque (i); Fre (i,n,n*2) {inque (i); Outque (i); if (sum[que[head]]>=sum[i-n]) ans++;} PF ("%d\n", ans);} return 0;}
HDU non-negative Partial Sums (monotone queue)