HDU 1024 Max Sum Plus Plus (Classic DP)

Keywords: Programming

HDU 1024 Max Sum Plus Plus (Classic DP)

Problem Description

    Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are faced with a more difficult problem.

    Given a consecutive number sequence
    S1,S2,S3,S4...Sx,...Sn(1xn1,000,000,32768Sx32767) .
    We define a function sum(i,j)=Si+...+Sj(1ijn).

    Now given an integer m(m>0), your task is to find m pairs of i and j which make sum(i1,j1)+sum(i2,j2)+sum(i3,j3)+...+sum(im,jm) maximal(ixiyjx or ixjyjx is not allowed).

    But I`m lazy, I don't want to write a special-judge module, so you don't have to output m pairs of i and j, just output the maximal summation of sum(ix,jx)(1xm) instead. ^_^

Input

    Each test case will begin with two integers m and n, followed by n integers S1,S2,S3...Sn.
    Process to the end of file.

Output

    Output the maximal summation described above in one line.

Sample Input

1 3 1 2 3
2 6 -1 4 -2 3 -2 3

Sample Output

6
8

Hint

Huge input, scanf and dynamic programming is recommended.

Main idea:

Give the number of m, N and n, divide the number of n into m groups, of course, you can also choose not to find the maximum of the sum of the m groups.

Explanation:

I believe everyone can see that this is a dp at a glance. In fact, this is a classic dp problem. Of course, there are many changes in HDU, such as HDU 1224, if you are interested, you can do it.
We define dp[i][j] to dendp [i] [j] to dendp [i] [j] to represent the largest sum of the previous J numbers divided into group I. We can get the transfer equation: dp[i][j] [j] = max (dp[i][j [i] [j [j\ [i] [j\ [j]]+a [j], Max (dp [i [i\ [i] [1] [k]]] [k]+a [k]]]+a [j]]))))), (0 < K [k <k [j ((((((([k [j j \\\Question n < 100000, consider optimization.
We can see that dp[i][j] is only related to all States of dp[i][j-1] and dp [i-1]. In fact, this is a relatively common routine. When dp is only related to the last state, we can record the previous optimal solution and then optimize an n. For example, here we record the maximum value of dp [i-1], so we don't need to traverse k.
At this time, we found that our space exploded, because once m was big, we defined 1e7 or higher, and considered optimization, because all the information of our DP [i-1] had been saved, so we could not use the state of DP [i-1], so we could give up and reduce it to one-dimensional dp, so this question came up happily.

Code:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <ctime>
#include <map>
#include <vector>
using namespace std;

inline int read() {
    int i = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9') {
        if(ch == '-') f = -1; ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        i = (i << 3) + (i << 1) + ch - '0'; ch = getchar();
    }
    return i * f;
}

const int MAXN = 1e6 + 10;
int dp[MAXN], mx[MAXN], a[MAXN];

int main() {
    int n, m;
    while(scanf("%d%d", &m, &n) != EOF) {
        for(int i = 1; i <= n; ++i) a[i] = read(), dp[i] = 0, mx[i] = 0;
        int x = 0;
        for(int i = 1; i <= m; ++i) {
            x = -0x3f3f3f3f;
            for(int j = i; j <= n; ++j) {
                dp[j] = max(dp[j - 1] + a[j], mx[j - 1] + a[j]);
                mx[j - 1] = x;
                x = max(x, dp[j]);
            }
        }
        printf("%d\n", x);
    }
}

At the end of this topic:

Thank you for reading this article. If you like it, please give me a compliment. Your encouragement is my greatest motivation.

If you have any comments, please let them out.

Posted by konqest on Tue, 01 Jan 2019 01:51:08 -0800