I. The origin of the article
At night a water ~~poj2479, a simple DP problem, review dynamic programming two, find the largest sub section and
The problem is to find the largest and most of the two segments in a sequence, which is the largest and only variant of the pure, such as the example given in the topic
1-1223-34-45-5
Inthesample,wechoose{2,2,3,-3,4}and{5},thenwecangettheanswer.
The answer is 13.
How to find the sub-segment of a sequence and: intmaxsub (INTA[],INTN)//This is only required maximum and the time complexity is O (n) {int*dp=newint (n); intmax,i; max=dp[0]=a[0]; for (i=1 i<n;i++) {if (dp[i-1]>0) dp[i]=dp[i-1]+a[i]; else dp[i]=a[i]; if (Dp[i]>max) max=dp[i]; DELETEDP; Returnmax; }
Analysis:
A very clear way of writing, each time judged, the current DP value (that is, the current maximum substring), as in the example:
1-1223-34-45-5
1024748494
This is very clear ~ ~ Third, the problem-solving report
First on the AC code: #include <iostream> #include <limits> usingnamespacestd; defineMAXN50002 INTMAP[MAXN]; INTLDP[MAXN]; INTRDP[MAXN]; Voidmaxlsub (INTA[],INTN)//This is required only the largest and the time complexity is O (n) {inti; LDP[0]=A[0]; for (i=1;i<n;i++) {if (ldp[i-1]>0) ldp[i]=ldp[i-1]+a[i]; else ldp[i]=a[i];} for (i=1;i<n;i++) Ldp[i]=ldp[i] <LDP[I-1]? Ldp[i-1]:ldp[i]; Voidmaxrsub (INTA[],INTN)//This is only required the largest and the time complexity is O (n) {inti; RDP[N-1]=A[N-1]; for (i=n-2;i>-1;i--) {if (rdp[i+1]>0) rdp[i]=rdp[i+1]+a[i]; else rdp[i]=a[i];} for (i=n-2;i>-1;i--) RDP[I]=RDP [I]<rdp[i+1]? Rdp[i+1]:rdp[i]; Intmain () {intt,n cin>>t; while (t--) {scanf ("%d", &n);//cin>>n; for (Inti=0;i<n;++i) {scanf ("%d") &map[i]); cin>>map[i]; } maxlsub (Map,n);//Generate DP Maxrsub (MAP,N); Intres=int_min; for (Inti=1;i<=n-1;++i) {inttmp=ldp[i-1]+rdp[i]; res=tmp>res?tmp:res;} cout<<res<<endl; } Return0; }
But this problem with violent maxsub (map,i) +maxsub (map+i,n-i) to do, is no use, will tle, then I will be cin to scanf, but also not, and then use this method: Intfindmax (INTLB,INTRB {intmax=int_min for (inti=lb;i<=rb;++i) {max=dp[i]>max?dp[i]:max;} Returnmax} intmain () {/////... for (INT I=1;i<=n-1;++i) {//inttmp=maxsub (map,i) +maxsub (map+i,n-i);//n^2 Inttmp=findmax (0,i) +findMax (i+1,n)-dp[i-1]; res=tmp>res?tmp:res; } /////...... }
The reason is that the use of Maxsub (map,i) +maxsub (map+i,n-i) Pure violence method, is the complexity of O (n^2), later converted to Findmax (0,i) +findmax (i+1,n)-dp[i-1] In fact, is still the complexity of O (n^2).
The AC code is used to find the maximum substring of the normal from left to right, and then use for (i=1;i<n;i++) ldp[i]=ldp[i]<ldp[i-1]? Ldp[i-1]:ldp[i];
Find the maximum value of the current point from left to right, if the value in the example is:
1-1223-34-45-5
1024748494
1124778899
Guaranteed from left to right, must be bigger, find a point can find the maximum value of this point to the left ~ ~
Similarly, you can calculate from right to left, a point to the right of the maximum value, so that the space to change time, but even so, only ran out of 438MS ...
and replaced scanf with cout and tle.
-end-