Maximum subsequences and

Source: Internet
Author: User

Maximum subsequences and

# Include

 
  
# Include

  
   
Int main () {int this_sum, max_sum, old_first, old_last, new_first; int n, I, tmp, flag = 1; int first = 1; int * data; scanf ("% d", & n); this_sum = max_sum = old_first = new_first = 0; old_last = n-1; data = (int *) malloc (n * sizeof (int); for (I = 0; I <n; I ++) {scanf ("% d", & data [I]);} for (I = 0; I <n; I ++) {this_sum + = data [I];/* accumulate to the right */if (first) // find the largest sum for the first time and {if (this_sum> = 0) {first = 0; old_first = old_last = I ;}} if (flag) // obtain the Beginning of the New this_sum {if (this_sum >=0) {flag = 0; new_first = I ;}} if (this_sum> max_sum) {max_sum = this_sum;/* if a larger sum is found, the current result is updated */old_last = I; if (new_first! = Old_first) {old_first = new_first;} else if (this_sum <0)/* if the current subsequence is negative */{this_sum = 0; /* cannot increase the following and discard it */flag = 1;} printf ("% d \ n", max_sum, data [old_first], data [old_last]); 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.