Max Sum plus plus max sub-segment and classic Dp

Source: Internet
Author: User

Kuangbin Special Links: Http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68966#problem/A

Test instructions: Maximum m sub-segment problem: the number of intervals given without crossing is obtained, and the maximum and value are obtained.

See, N reached 1000000, the heart of the first idea is only one-dimensional DP problem, but in the derivation of the state transfer formula, you have to use two-dimensional DP thought.

See the online algorithm time complexity of O (MN), it seems that M is not very large.


In fact, it is easy to think of a state transition equation: Dp[i][j]: Indicates the maximum and value of J for the interval number at the first position.

There are two kinds of transfer situations:

One: Dp[i][j], the last sub-segment J (value equals the number of child segments) in Dp[i][j], then dp[i][j]=dp[i-1][j];

Second: Dp[i][j] in the last sub-segment J (value equals the number of sub-segments) in Dp[i][j], at this point, it is easy to think: The end of the last sub-paragraph must be enumerated at the beginning of the last subsection, the state transition equation is: Dp[i][j]=max (dp[k][ J-1]+s[i]-s[k]), where s represents the prefix and, 1<k<i. At first I was thinking like this, but I couldn't do it. Look at the puzzle: At this point, the last position J is divided into two cases:

1:DP[I][J] The last position I belongs to Dp[i-1][j] The last sub-segment J, i.e.: equivalent to placing position I into the tail of dp[i-1][j], then: Dp[i][j]=dp[i-1][j]+a[i].

The last position of 2:dp[i][j] I does not belong to the last sub-segment J of Dp[i-1][j], i.e.: The last position belongs to a separate sub-segment, then: Dp[i][j]=dp[k][j-1]+a[i]. 1<=k<i;

At this point the transfer equation comes out, but N is too big to open a two-bit array. Said to use the scrolling array, I actually do not use the scrolling array, refer to other people's code, and then write it again.

Freopen ("C:\\Documents and Settings\\All Users\\ desktop \\in.txt", "R", stdin);

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>
# include<vector>
#include <map>
#include <string>
#define LL __int64
#define INF 0x7fffffff

using namespace std;

int dp[1000010],p[1000010],a[1000010],m,n;

int main () {while
    (cin>>m)
    {
        cin>>n;
        for (int i=1;i<=n;i++) scanf ("%d", &a[i]);
        Memset (Dp,0,sizeof (DP)), memset (P,0,sizeof (P));
        int Max;
        for (int i=1;i<=m;i++) {
            max=-inf;
            for (int j=i;j<=n;j++)//Obviously, J must >=i
            //Scroll the array is in time to recycle the unused space behind, here is the P array
                dp[j]=max (Dp[j-1],p[j-1]) +a[j], P[j-1]=max,max=max (Dp[j],max);
        }
        cout<<max<<endl;
    }
    return 0;
}


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.