Acdream 1682 (maximum continuous sum of loops)

Source: Internet
Author: User

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 ;}

 


 

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.