HDU Non-negative Partial Sums (單調隊列)
Problem Description You are given a sequence of n numbers a
0,..., a
n-1. A cyclic shift by k positions (0<=k<=n-1) results in the following sequence: a
k a
k+1,..., a
n-1, a
0, a
1,..., a
k-1. How many of the n cyclic shifts satisfy the condition that the sum of the fi rst i numbers is greater than or equal to zero for all i with 1<=i<=n?
Input Each test case consists of two lines. The fi rst contains the number n (1<=n<=10
6), the number of integers in the sequence. The second contains n integers a
0,..., a
n-1(-1000<=a
i<=1000) representing the sequence of numbers. The input will 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 stated above.
Sample Input
3
2 2 1
3
-1 1 1
1
-1
0
Sample Output
3
2
0
通過這個題才對單調隊列有一點了解
題意:一個數列,每一次把第一個數放到尾部,判斷這個數列對於任意的 i (1<=i<=n) 是否都滿足 sum[i]>=0,如果滿足ans++,求最大的ans
思路:先把串加倍,隊列需要維護長度為n的序列中的最小值放在隊首
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include