The algorithm of three version below is essentially the same, namely, Kadane ' s algorithm, which are of O (n) complexity. Https://en.wikipedia.org/wiki/Maximum_subarray_problem
The evolution of the implementations is to remove redundancy and does what are really needed, the side effect of doing so are becoming more efficient.
IMHO, version 1.0.2 is well commented which shows the guidelines of efficient bookkeeping of boundaries, first and last CA n also is easily changed to First_iter and last_iter to print the whole subarray if needed.
Version 1.0.0, a coordinate array, a traversing to find the first element
#include <cstdio>#include <algorithm>#define MAXSIZE 10005intMain () {#ifndef Online_judgeFreopen ("In.txt","R", stdin);#endif intN,i,tmp,*p;intnums[maxsize]={-1}, dp[maxsize]={0}; while(SCANF ("%d", &n) = =1&& n>0) { for(i=1; i<=n;++i) {scanf ("%d", &nums[i]); } for(i=1; i<=n;++i) {tmp=nums[i]+ (dp[i-1]>0? dp[i-1]:0); Dp[i]=tmp>0? tmp:0;//DP[I]=std:max (0, Nums[i]+std::max (dp[i-1],0)); } p=std::max_element (dp,dp+n+1);if(P==DP) {if(Nums==std::max_element (nums,nums+n+1)) {i=1, Tmp=n; }Else{ for(i=0; I<=n && nums[++i]<0;) {} for(tmp=i;nums[++tmp]==0;) {}--tmp; } }Else{ for(tmp=i=p-dp;i>0&& dp[--i]>0;) {} for(;i>0&& nums[i]==0&& dp[--i]==0;) {} ++i; }printf("%d %d%d \ n",*p, Nums[i],nums[tmp]); }return 0;}
Version 1.0.1, no coordinate array, modifying the data, a traversing to find first element
#include <cstdio>#include <algorithm>#define MAXSIZE 10005intMain () {#ifndef Online_judgeFreopen ("In.txt","R", stdin);#endif intN,i,j,tmp, Last, sum;intnums[maxsize]={-1}; while(SCANF ("%d", &n) = =1&& n>0) { for(i=1; i<=n;++i) {scanf ("%d", &nums[i]); } for(sum=-1, Last=nums[n],j=0, i=1; i<=n;++i) {tmp=nums[i]+ (nums[i-1]>0? nums[i-1]:0);if(tmp>=0) {if(tmp>sum) {sum=tmp; Last=nums[i]; J=i; } nums[i]=tmp; } }if(sum==-1) ++sum;Else for(;j>0&& nums[--j]>=0;) {}printf("%d %d%d \ n", sum,nums[j+1], Last); }return 0;}
Version 1.0.2, no coordinate array, not modify data, no extra traversing to find boundary element
#include <cstdio>#include <algorithm>#define MAXSIZE 10005intMain () {#ifndef Online_judgeFreopen ("In.txt","R", stdin);#endif //prev--maxsum ending here, sum--maxsum so far, res--result intN,i,first,last,tmp,sum, Res,prev;intNums[maxsize]; while(SCANF ("%d", &n) = =1&& n>0) { for(i=0; i<n;++i) {scanf ("%d", &nums[i]); } for(res=prev=sum=-1, first=nums[0],last=nums[n-1], i=0; i<n;++i) {if(prev<0) {if(nums[i]>=0) {//prev Start increasing, update candidate of first-TMPTmp=prev=nums[i];//Update candidate of result--sum if(prev>sum) {sum=prev; } } }Else{Prev+=nums[i];//Prev Stop increasing, update first, last, Res if(nums[i]<=0) {if(sum>res) {res=sum; first=tmp; last=nums[i-1]; } }//Update candidate of result--sum Else if(prev>sum) {sum=prev; } } }//Update First, last, Res,--only if partial sum remain increasing if(sum>res) {res=sum; first=tmp; last=nums[i-1]; }//All negative if(res==-1) ++res; printf"%d%d%d\n", res,first,last); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu 1231, maximum consecutive sum of integers, finding the boundaries, possibly all negative, C + +