A one-dimensional array that evaluates the maximum number of sub-arrays (first adjacent 32 bits)

Source: Internet
Author: User

Returns the and of the largest sub-arrays in an integer array, and refines the analysis:
1, in all sub-arrays ending in element tail, select the element and the largest sub-array, TAIL=1,2...N.
2, and the largest sub-array ending with element K is the TAIL-1 that contains the end of the element and the largest subarray, or only the element tail this element, there are altogether these two optional States.

3, after getting the maximum sub-array at the end of each element, as long as the maximum is the maximum of all the sub-arrays

4, the type of the array element is defined as _int32, and the array element is randomly generated.

The code is as follows:

//Euna Lee Wang Shuo 2016.3.25#include <iostream>#include<algorithm>#include<time.h>#defineRAND16 (rand () <<1) + (rand () &1))# define WIDE4294967296using namespacestd;#defineMAX 1000Long Long intA[max];Long Long intTail[max]; //dynamic programming ideas, Time complexity O (n)intMain () {intLength//Array Length    intI//Loop Variable    intTail//record where the array endsSrand ((_int32) time (NULL)); cout<<"Please input the length of array:"<<Endl; CIN>>length; cout<<"randomly generated."<<length<<"number is"<<Endl;  for(i=1; i<=length; i++) {A[i]= (RAND16 << -) + RAND16;//generating 32-bit array elementscout<<a[i]<<Endl; }                                                                //calculates the maximum value of the sum of the sub-arrays ending in tail, that is, the sub-array contains the number of Ktail[1] = a[1];  for(tail=2; tail<=length; tail++)//Tail a stage{Tail[tail]= Max (a[tail],tail[tail-1]+A[tail]); }                                              //only two states//because and the largest subarray must end with a certain number, the maximum value of this length sub-array is taken    Long Long intall = tail[1];  for(i=2; i<=length; i++) All=Max (all, tail[i]); cout<<"MAX:"<<All<<"  !"<<Endl;} 

Result

A one-dimensional array that evaluates the maximum number of sub-arrays (first adjacent 32 bits)

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.