CSU 1529 Equator DP
Queue optimization DP
Equator
Time Limit:5000 MS |
|
Memory Limit:131072KB |
|
64bit IO Format:% Lld & % llu |
Submit Status
Description
Input
Output
Sample Input
3 3 1 2 3 8 4 5 -1 -1 1 -1 -1 5 2 -1 -1
Sample Output
6 14 0
/*************************************** * ******** Author: CKbossCreated Time: Tuesday, March 17, 2015 File Name: CSU1529.cpp *************************************** * *********/# include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; const int maxn = 2000200; int n, ans; int a [maxn]; int sum [maxn]; int dq [maxn], head, tail; int main () {// freopen ("in.txt", "r", stdin); // freopen ("out.txt", "w", stdout); int T_T; scanf ("% d", & T_T); while (T_T --) {scanf ("% d", & n); for (int I = 1; I <= n; I ++) {scanf ("% d", a + I); a [n + I] = a [I] ;}for (int I = 1; I <= 2 * n; I ++) sum [I] = sum [I-1] + a [I]; head = 0; tail =-1; ans = 0; for (int I = 1; I <= 2 * n; I ++) {while (head <= tail) & (sum [I]-sum [dq [tail] <0) {ans = max (ans, sum [dq [tail]-sum [dq [head]); tail --;} while (head <= tail) & (I-dq [head]> n) {ans = max (ans, sum [dq [tail]-sum [dq [head]); head ++;} dq [++ tail] = I;} printf ("% d \ n", ans);} return 0 ;}