! HDU Max Sum Plus plus-dp-(group DP?) Maximum sub-sequence and)

Source: Internet
Author: User

Test instructions: N number, divided into M-segment, to find the maximum of the M segment and, between the segments can not cross.

Analysis:

The difference between this problem and the maximal subsequence is that the requirements are divided into M-segments, so the approach is different. The actual practice is somewhat like a packet DP (the number of n is divided into M-groups), but the topic and the last written two group DP (moving bedroom and special chopsticks) The difference is: The last two questions each group selected two or three elements, the subject is not sure how many elements to choose each paragraph, the first two questions sorted and then do, the problem can only be ordered according to the sequence of the order to go.

The difficult point:

1. Status: Dp[i][j] Under the premise of determining the J element, the maximum of the first J sub-section I; All for the J element there are two cases: 1). Join the last paragraph in the identified I section, 2). To start over again, you need to enumerate who is the last element of paragraph i-1, find and Max , and this is done here, although it seems that there is no case of the J element being selected when the state transitions.               As for this question, why not use the normal packet DP State transfer: 1). Select A[j] To join an existing paragraph, 3). Select A[j] To start a new paragraph, that is because each paragraph here must be continuous, and if so, but discontinuous, discontinuous is not the new Is the paragraph coming out again?

Transfer: Dp[i][j]=max (Dp[i][j-1]+a[j],dp[i-1][k]+a[j]), I-1<=k<j-1 (j-1 is open interval, because if it is a closed interval, element J and J-1 is actually a paragraph);

2. Optimization:

1). Space: Start with the scrolling array I used, WA, the scrolling array is not too much to use, and later attention accumulates. The method used here is to replace the two-dimensional

2). Time: Directly with the above transfer equation is the triple loop, time-out, here a new array to save the previous dp[i-1][k], reduce a heavy cycle, into a double loop

It's more difficult.

Code:

#include <iostream> #include <cstring> #define INF 1000000007using namespace Std;int k,n,a[1000010],mx;int Dp[1000010];int pre[1000010];int Max (int i,int j) {return i>j?i:j;} void DP () {for (int i=1;i<=k;i++) {    mx=-inf;for (int j=i;j<=n;j++) {Dp[j]=max (dp[j-1]+a[j],pre[j-1]+a[j]); Pre[j-1]=mx;mx=max (Mx,dp[j]);  Pre[j-1]=max (Pre[j-1],dp[i%2][j]);}} int main () {while (cin>>k>>n) {for (int i=1;i<=n;i++) Cin>>a[i];memset (Dp,0,sizeof (DP)); Memset ( Pre,0,sizeof (pre));//for (int i=0;i<=n;i++) Pre[i]=-inf;    DP ();//cout<<dp[k%2][n]<<endl;    Cout<<mx<<endl;}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

! HDU Max Sum Plus plus-dp-(group DP?) Maximum sub-sequence 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.