Divide-and-conquer method to find the maximum field and

Source: Internet
Author: User

Question: A sequence of n integers (possibly negative numbers) a[1],a[2],a[3],..., A[n], and the maximum value of the sub-segments of the sequence, such as A[i]+a[i+1]+...+a[j], is obtained. When the given integer is negative, the defined sub-segment and is 0, according to this definition, the optimal value is: Max{0,a[i]+a[i+1]+...+a[j]},1<=i<=j<=n For example, when (A[1],a[2],a[3],a[4],a[5],a[6] ) = ( -2,11,-4,13,-5,-2), the maximum sub-segment and is 20.

Divided into three cases, divided by the middle, 1. All on the left, 2. All on the right 3. Across the middle mid (which is easy to find)

The situation can be recursive according to the original problem

//MaxChildArray.cpp:Defines the entry point for the console application.//#include"stdafx.h"#include<VECTOR>#include<iostream>using namespacestd;//return value,result[0]-> lindex result[1]-> rindex result[2]-> sum (lindex,rindex)Vector<int> Getmaxacross (vector<int> & V,intMidintBeginintend) {    intLsum =0, maxlsum=-9999; intRsum =0, maxrsum=-9999; Vector<int> Result (3,0); inti =0;  for(i = mid;i>=begin;i--) {lsum+=V[i]; if(Maxlsum <lsum) {Maxlsum=lsum; result[0] =i; }    }     for(i = mid+1; i<=end;i++) {rsum+=V[i]; if(Maxrsum <rsum) {Maxrsum=rsum; result[1] =i; }} result[2] = maxlsum+maxrsum; returnresult;} Vector<int> Getmaxchildarray (vector<int> & V,intBeginintend) {Vector<int> Result (3,0); if(Begin = =end) {//recursive cutoff point result[0] =begin; result[1] =end; result[2] =V[end]; returnresult; }    intMid = (begin+end)/2; Vector<int> lresult =Getmaxchildarray (V,begin,mid); Vector<int> Rresult = Getmaxchildarray (v,mid+1, end); Vector<int> Midresult =Getmaxacross (v,mid,begin,end); if(lresult[2] >= rresult[2] && lresult[2] >= midresult[2])        returnlresult; if(rresult[2] >= lresult[2] && rresult[2] >= midresult[2])        returnRresult; returnMidresult;}intMainintargcChar*argv[]) {    intArray[] = {-2, One,-4, -,-5,-2}; Vector<int> Elements (array,array+sizeof(array)/sizeof(int)); Vector<int> result = Getmaxchildarray (elements,0,sizeof(array)/sizeof(int)-1); cout<<"Leftindex ="<<result[0]<<Endl; cout<<"Rightindex ="<<result[1]<<Endl; cout<<"Maxsubarray ="<<result[2]<<Endl; inti; CIN>>i; return 0;}

Divide-and-conquer method to find the maximum field and

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.