From O (n^3) to O (n) for maximum continuous and

Source: Internet
Author: User

Maximum continuity and problem: give a sequence of length n A1, A2, a3, An, seeking maximum continuous and. Or this understanding: Ask to find 1≤i≤j≤n, make ai+ ai+1 + +aj as large as possible.

Analysis

The easiest thing to think about at this time is the violence enumeration,,,

The code is as follows:

1  for(intI=1; i<=n; i++) {2      for(intJ=i; j<=n; J + +) {3         intsum =0;4          for(intK=i; k<=j; k++)5Sum + =Val[k];6         //Max = max>sum? Max:sum;7         if(Sum > Max) {//The subscript is recorded here, and if you don't need one, you can change it to the above sentence.8Max =sum;9II =i;TenJJ =J; One         } A     } -}

Obviously, this is an O (n^3) algorithm, the maximum range of theory N is 1000, which is actually about 1400 or so.

How to optimize it, let's think about this algorithm: set si = A1 + A2 + A3 + + Ai, then ai+1 + ai+2 + + Aj = sj-si-1

The implication is that "the sum of successive sub-sequences equals two prefixes and the difference", we can easily write the following code:

1? memset (Sum,0,sizeof(sum));2  for(intI=1; i<=n; i++) Sum[i] = sum[i-1] +Val[i];3  for(intI=1; i<=n; i++)4 for(intJ=i; j<=n; J + +)5max = max (max, sum[j]-sum[i-1]);

Its time complexity is O (n^2), although a layer less cycle, but the complexity is still very high

Think about whether you can optimize, O (NLOGN) algorithm has what,,, yes, is divided treatment

We first divide the sequence into two halves as equal as possible, find the best sequence on the left or completely on the right, and then find the maximum continuity and sequence at the left and end of the right, and compare the optimal solution to the sub-problem.

The code is as follows:

1?intSolveint*val,intXinty) {2     if(Y-x = =1)returnVAL[X];//there is only one element that returns3     intm = x + (y-x)/2;//dividing [x, M), [M, Y]4     intmax_s =Max (Solve (Val, X, M), solve (Val, M, y));5     intV, L, R;6v =0; L = val[m-1];7      for(inti=m-1; i>=x; i--) L = max (L, v+=val[i]);8v =0; R =Val[m];9      for(intI=m; i<=y; i++) R = Max (R, v+=val[i]);Ten     returnMax (max_s, L +R); One}

The algorithm is O (NLOGN), has been able to pass a large data, but there is no better way?

The answer, of course, is that the O (n) algorithm is smaller than O (Nlogn), and there is more than one:

1?intsolve_1 () {2     intN scanf"%d", &n);3     intNow; scanf"%d", &now);//Read first number first4     intTemp, Ans = now;//first number to Ans5     if(Now >0) Temp = now;//If the first number is positive, then there is value maintenance6      for(intI=2; i<=n; i++) {7scanf"%d", &Now );8Temp + = Now;//before calculation? A continuous element and9         if(Temp > Ans) Ans = Temp;//There is a better solution, updateTen         if(Temp <0) Temp =0;//if the previous and less than 0, there is no value for maintenance One     } Aprintf"%d\n", Ans);//Output Answer -}
1?voidsolve_2 () {2     intMax =-99999, ANS[MAXN];3memset (Ans,0,sizeof(Ans));4     intN scanf"%d", &n);5      for(intI=1; i<=n; i++) {6         intX scanf"%d", &x);7Ans[i] = max (ans[i-1]+x, x);//DP8Max =Max (max, ans[i]);9     }Tenprintf"%d\n", Max); One}

From O (n^3) to O (n) for maximum continuous 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.