Acdream 1682 (maximum continuous sum of loops)
Question: There are n numbers in a circle, and then take some continuous numbers from the circle, and ask the maximum value of the sum of the numbers taken.
Solution: The general maximum continuity method. If the current number of the accumulated number plus is greater than the maximum value, the maximum value is updated. If it is less than 0, the accumulated value is cleared. This has a ring, we can consider two situations: one is the normal maximum continuous and the found maximum, and the other is the first and the last are concatenated to take the opposite number of all numbers, then find the maximum continuous sum, then add this number with sum is the maximum value of the head and tail stitching, take two cases of greater is the solution.
# Include
# Include using namespace std; const int N = 200010; int n, s [N]; int main () {int t; scanf ("% d", & t ); while (t --) {scanf ("% d", & n); int sum = 0; for (int I = 1; I <= n; I ++) {scanf ("% d", & s [I]); sum + = s [I];} int res = 0, maxx = 0; for (int I = 1; I <= n; I ++) {res + = s [I]; if (res> maxx) maxx = res; if (res <0) res = 0 ;} for (int I = 1; I <= n; I ++) s [I] =-1 * s [I]; int res2 = 0, maxx2 = 0; for (int I = 1; I <= n; I ++) {res2 + = s [I]; if (res2> maxx2) maxx2 = res2; if (res2 <0) res2 = 0;} printf ("% d \ n", max (maxx, sum + maxx2);} return 0 ;}