HDU-1024 Max Sum plus Plus (DP + scrolling array)

Source: Internet
Author: User

The main problem: to find the largest and most of the non-intersecting intervals

Problem-solving ideas: This is a reference to others, portal

Organize yourself again, using dp[i][j] to indicate that the first J arrays comprise the largest of the I disjoint intervals, where the J number must be within a certain interval
Now, the decision is that the J-array is in a range of numbers and the other number, or is it a separate section of its own.
1. If the interval is in and other numbers, then dp[i][j] = dp[i][j-1] + num[j]
2. If the interval is independent, then dp[i][j] = Dp[i-1][k] + num[j]
Dp[i-1][k] = max (dp[i-1][i-2], dp[i-1][i-1] ... dp[i-1][j-1])

In summary, dp[i][j] = max (dp[i][j-1], dp[i-1][k]) + num[j]
Where Dp[i-1][k] can be recorded with a variable, record the maximum value of each dp[i-1][k]
Because each DP is only related to the previous one, it can be turned into a rolling array to solve the

 #include <cstdio> #include <cstring> #include <algorithm> using namespace
Std
const int N = 1000010;

const int INF = 0x7fffffff;
int n, m;

int dp[n][2], num[n], mmax[n];
        void Solve () {for (int i = 1; I <= n; i++) {scanf ("%d", &num[i]);
    Dp[i][0] = dp[i][1] = 0;

    } Dp[0][0] = dp[0][1] = 0;
    int Max;
        for (int i = 1; I <= m; i++) {Max =-inf;
            for (int j = i; J <= N; j + +) {dp[j][0] = max (dp[j-1][0] + num[j], dp[j-1][1] + num[j]);
            DP[J-1][1] = Max;
        max = max (max, dp[j][0]);
}} printf ("%d\n", Max);
    } int main () {while (scanf ("%d%d", &m, &n)! = EOF) solve ();
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.