HDU max sum plus Plus

Source: Internet
Author: User

Just write this question know is DP but dynamic transfer equation can't push out, and later on the internet searched the problem.

Simplify the problem first, if you choose one, this is the classic Max sum DP.

The state transfer equation is dp[i]=dp[i-1]+a[i]>a[i]?:d P[i-1]+a[i]:a[i];

Title: http://acm.hdu.edu.cn/showproblem.php?pid=1003

Besides this problem http://acm.hdu.edu.cn/showproblem.php?pid=1024;

Test instructions is the maximum value of multiple sub-columns that you do not want to cross and continue. From a section to find the largest and then can think of open a two-dimensional array dp[i][j]; Each state can be understood to be divided into the I paragraph when the J element is selected when the maximum value, so either the number of J is selected on the basis of the previous layer of the new section J, or on the basis of this layer to add the number of J, and Nature will be the largest, so the state transfer equation Dp[i][j]=max (Dp[i][j-1]+a[j],max (Dp[i-1][k]) +a[j] (i-1<=k<j));

Due to the range is very large <=1000000; open 2-dimensional array burst memory, so whether one-dimensional can, I first think is open two array alternating, anyway only and this layer and the upper level, but too troublesome, to alternate assignment, time is expensive.

No time the update needs to use the previous level to i-1~j-1 the maximum value, so you can open the number of records, the equation becomes Dp[j]=max (dp[j-1]+a[j],pre[j]+a[j]);

Finally find Dp[n]~dp[p] the largest on the line, N is the segment to be divided, p is the total number of elements.

Here's a look at two pieces of code:

1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <string.h>5#include <stdlib.h>6#include <math.h>7 using namespacestd;8typedefLong Longll;9ll a[1000005];Tenll aa[1000005];//DP Array Onell b[1000005];//array of maximum values for record i-1~j-1 A //ll pp[1000005]; - intMainvoid) - { the ll I,j,k,p,q,l,n,m; -      while(SCANF ("%lld", &m)! =EOF) -     { -scanf"%lld",&p); +         if(m>p)//m cannot exceed P -         { +m=p; A         } at          for(i=1; i<=p; i++) -         { -scanf"%lld",&a[i]); -         } -  -aa[0]=0; inmemset (b,0,sizeof(b));//every time B is initialized -          for(i=1; i<=m; i++) to         { +              for(j=i; j<=p; J + +) -             { theaa[j]=aa[j-1]+a[j]>b[j]+a[j]?aa[j-1]+a[j]:b[j]+A[j]; *  $             }Panax NotoginsengB[i]=aa[i];//after this layer cycle, update the B array every update to start with the number of I, because to be divided into i+1 segment must be divided into the I segment to add a number, and the Division I must be the number of I.  -              for(j=i+1; j<=p; J + +) the             { +b[j]=aa[j-1]>b[j-1]?aa[j-1]:b[j-1]; A             } the         } +ll maxx=Aa[m]; -  $          for(i=m; i<=p; i++) $         { -             if(maxx<Aa[i]) -             { themaxx=Aa[i]; -             }Wuyi         } the  -printf"%lld\n", Maxx); Wu  -     } About  $     return 0; -  -}
1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <string.h>5#include <stdlib.h>6#include <math.h>7 using namespacestd;8typedefLong Longll;9ll a[1000005];Tenll aa[1000005]; Onell b[1000005]; All pp[1000005]; - intMainvoid) - { the ll I,j,k,p,q,l,n,m; -      while(SCANF ("%lld", &m)! =EOF) -     { -scanf"%lld",&p); +         if(m>p) -         { +m=p; A         } at          for(i=1; i<=p; i++) -         { -scanf"%lld",&a[i]); -         } -  -aa[0]=0; inmemset (b,0,sizeof(b)); -          for(i=1; i<=m; i++) to         { +              for(j=i; j<=p; J + +) -             { theaa[j]=aa[j-1]+a[j]>b[j]+a[j]?aa[j-1]+a[j]:b[j]+A[j]; *                 if(j==i) $                 {Panax Notoginsengb[j]=Aa[i]; -                 } the                 Else +                 { Ab[j]=aa[j-1]>b[j-1]?aa[j-1]:b[j-1];//here to optimize the update of the B directly and the AA update merge does not have to be in a heavy, improve the efficiency of the Code.  the                     //because it is currently updated AA[J] so b[j] record is i-1 to j-1 the maximum update after AA and then update B does not affect the aa[j+1] update b[j+1] or the last layer of the update before the layer changed.  +                 } -  $             } $  -         } -ll maxx=Aa[m]; the  -          for(i=m; i<=p; i++)Wuyi         { the             if(maxx<Aa[i]) -             { Wumaxx=Aa[i]; -             } About         } $  -printf"%lld\n", Maxx); -  -     } A  +     return 0; the  -}

HDU max sum plus Plus

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.