HDU 1231, maximum consecutive sum of integers, finding the boundaries, possibly all negative, C + +

Source: Internet
Author: User

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 + +

Related Article

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.