Introduction to Algorithms learning-sub-arrays Max and problem

Source: Internet
Author: User

1#include <iostream>2#include <algorithm>3 using namespacestd;4 Const intMaxsize=101;5 intA[maxsize],sum[maxsize],n,inf= (1<< -);6 voidsolve () {7     if(n==0)return;8     intans=-inf;9sum[1]=a[1];Ten     intleft=1, right=1; One      for(intI=2; i<=n;i++){ A         if(sum[i-1]>0){ -sum[i]=sum[i-1]+A[i]; -right++;  the         } -         Else{ -sum[i]=A[i]; -left=i; +         } -         if(Sum[i]>ans) ans=Sum[i]; +     } Aprintf"Subarray from Indice%d->%d have the maximum sum:%d\n", Left,right,ans);  at } - intMain () { -      while(SCANF ("%d", &n)! =EOF) { -          for(intI=1; i<=n;i++) scanf ("%d",&a[i]); - solve (); -         /* in Maxisum-subarray problem, in fact, was try to find Subarray A[I...J] - of array a[1..i...j. N] (1<=i<=j<=n). and the problem is meaingful to Only if array A has both the positve value and negative elements. +              - considering the size of problem: the * If n==1, the problem is quite easy. A[1] is the right answer.  * * If array A has more than one value and then we can use DP method to $ solve it.Panax Notoginseng              - Considering the procedure have already checked the a[i-1], and is the going to enter the i th check. And we can assume that we have + already get the maximum subarray of the a[1...i-1].supposing the A maxisum Subarray of A[1..i-1] is a[p. I-1]. the              + we have aleady get procedure are going to the ith check - the solution of a[1..i], and we'll move to check a[i]. $ relationship between Sum[i-1] and Sum[i]? From our experience, $               - * If sum[i-1]<0, then whatever the A[i) is, the sum of Subarray - A[p...i-1,i] must is smaller than that of Subarray a[p. I-1]. So we the Modify the starting indice of Maxisum from ' P ' to ' I '. and at the - same time, we can know that the maxisum subarray of a[1...i] are notWuyi The a[p...i] but the a[p...i-1]. So the value of Maxisum remains the the same:the sum of a[p...i]. - * For another circumstance, if sum[i-1]>0, we can take both situations Wu int consideration, if a[i]>0, obviously, we should let - Sum[i]=sum[i-1]+a[i]. Then for a[i]<0? In fact, we also need to plus About A[i] onto sum[i-1]. As we know, in the (i-1) th calculation, acoording $ to the above assumption, we had already found the solution to the - Maxisum Subarray:a[p...i-1]. So although the sum of a[p...i-1] is - higher than that's a[p...i], in fact, with each iteration, we compare - The value of sum[i-1] and Sum[i] to the Maxinum and store the maxinum. A So we let the Sum[i-1]+a[i] have no matter to the maxinum. +              the (Sum[i] stores the max sum of Subarray end in A[i]).  -         */ $     } the     return 0; the}

Introduction to Algorithms learning-sub-arrays Max and problem

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.