Maximum continuous sequence and, product, and disjoint array and the difference between the largest

Source: Internet
Author: User

Problem One: The maximum number of sub-arrays and:

Use F[i] to denote the largest continuous subsequence and at the end of A[i]. I find the maximum value between 0~n-1 and the last comparison of all F[i]. For such a dynamic plan, you can simplify the update by scrolling through a variable F.

intMax_sum (intA[],intN) {    intf=a[0]; intmax_value=a[0];  for(intI=1; i<n;i++)    {        if(f<0) F=A[i]; ElseF=f+A[i]; Max_value=Max (max_value,f); }    returnMax_value;}

Question two: The maximum value of the disjoint subarray and its difference.

We need to divide the array into two disjoint parts, one for each possible two-part sub-array and the maximum value, and the minimum value. For example, the array a[0~n] is divided into a[0~i] and a[i+1~n], we use the f1[i] to represent the first part of the a[0~i] sub-array of the largest continuous sub-sequence and. F2[i] represents the and of the smallest contiguous subsequence of the first part of the a[0~i] sub-array. G1[I+1] represents the largest contiguous subsequence of the second part of the a[i+1~n] sub-array and. G2[i+1] represents the and of the smallest contiguous subsequence of the second part of the a[i+1~n] sub-array. Finally, for each I, we find the difference between the largest two parts, Max (f1[i]-g2[i+1], g1[i+1]-f2[i]), where f1[i],f2[i],g1[i],g2[i] is the result obtained on the basis of problem one;

intMax_diff_sum (intA[],Const intN) {Vector<int> f1 (n);//F1[i] Represents the maximum value of all successive sub-sequences contained in the A[0~i] sub-arrayvector<int>F2 (n); Vector<int>G1 (n); Vector<int>G2 (n); f1[0]=a[0]; f2[0]=a[0]; intmax_left_until=a[0];//represents the maximum number of consecutive sub-sequences ending with a[i]    intmin_left_until=a[0];//represents the minimum value of a continuous subsequence ending with a[i]     for(intI=1; i<n;i++)    {        if(max_left_until>0) Max_left_until+=A[i]; ElseMax_left_until=A[i]; F1[i]=max (f1[i-1],max_left_until); if(min_left_until<0) Min_left_until+=A[i]; ElseMin_left_until=A[i]; F2[i]=min (f2[i-1],min_left_until); } g1[n-1]=a[n-1]; G2[n-1]=a[n-1]; intmax_right_until=a[n-1]; intmin_right_until=a[n-1];  for(inti=n-2; i>=0; i--)    {        if(max_right_until>0) Max_right_until+=A[i]; ElseMax_right_until=A[i]; G1[i]=max (g1[i+1],max_right_until); if(min_right_until<0) Min_right_until+=A[i]; ElseMin_right_until=A[i]; G2[i]=min (g2[i+1],min_right_until); }    intresult=int_min;  for(intI=0; i<n-1; i++) {result=max (Result,max (f1[i]-g2[i+1],g1[i+1]-f2[i])); }    returnresult;}

Question three: maximal continuous product substring:

For a substring of the maximum continuous product of an array, because the product may have a positive negative, if the preceding product is negative, just a[i] is negative, it will be negative. So we need max_until to save the maximum value of the product of the substring ending with a[i], and the minimum value of the product of the substring with Min_until to the end of the a[i]. Then the maximum value in all the Max_until is the final result.

DoubleMax_multi (DoubleA[],intN) {    Doublemax_until=a[0]; Doublemin_until=a[0]; Doubleresult=a[0];  for(intI=1; i<n;++i) {Doubletemp1=max_until*A[i]; Doubletemp2=min_until*A[i]; Max_until=Max (Max (TEMP1,TEMP2), a[i]); Min_until=min (min (TEMP1,TEMP2), a[i]); Result=Max (result,max_until); }    returnresult;}

Maximum continuous sequence and, product, and disjoint array and the difference between the largest

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.