Test instructions: There are n numbers in a circle, and then take a number of consecutive numbers from the circle, asking what the maximum value of the number to take away is.
Solution: Ordinary maximum continuous and the practice, if the previous cumulative number plus the current number is greater than the maximum value to update the maximum value, if less than 0 to zero the accumulated value, this is a ring, then you can consider from two cases, one is the normal maximum continuous and find the maximum, the other is the head and tail stitching, the number of the opposite number And then find the maximum continuous and, then the sum sum plus this number is the maximum value of the top and tail stitching, take two kinds of cases larger is the solution.
#include <stdio.h> #include <algorithm>using namespace std;const int n = 200010;int N, s[n];int main () {int t;s CANF ("%d", &t), while (t--) {scanf ("%d", &n), int sum = 0;for (int i = 1; I <= n; i++) {scanf ("%d", &s[i]); su M + = 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;}
Acdream 1682 (maximum continuous and with ring)