Acdream 1682 (maximum continuous and with ring)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.