Dynamic planning HDU-1024

Keywords: PHP

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

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 S 1, S 2, S 3, S 4 ... S x, ... S n (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ S x ≤ 32767). We define a function sum(i, j) = S i + ... + S j (1 ≤ i ≤ j ≤ n). 

Now given an integer m (m > 0), your task is to find m pairs of i and j which make sum(i 1, j 1) + sum(i 2, j 2) + sum(i 3, j 3) + ... + sum(i m, j m) maximal (i x ≤ i y ≤ j x or i x ≤ j y ≤ j x 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(i x, j x)(1 ≤ x ≤ m) instead. ^_^ 


The meaning of the question is to find the maximum value of the sum of n numbers m segments

Using dp[n][m] to express the maximum sum of N blocks divided by the number of first m blocks

The transfer equation is dp[n][m]=max(dp[n][m-1]+a[m],max(dp[n-1][t])+a[m]), where dp[n][m-1]+a[m] means that the m-th block in the first i block after the m-th block represents that it becomes a block independently (1 < = T < = m-1).

So if the complexity of this method is (n^3), we need to simplify and repeat some procedures when we find max(dp[n-1][t]), so we only need to change it to set a variable M to represent the maximum value M=max(M,dp[i-1][j-1]);

But the space is not enough, so we use a rolling array dp[2][MAN]; i-1 is used in M;

Full code:

#include <stdlib.h>
#include<iostream>
#include<algorithm>
//#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<cmath>
#define ll long long
const ll INF=0x3f3f3f3f;
#define mod 1000000007
#define mem(a,b) memset(a,b,sizeof(a))
//__builtin_popcount
using namespace std;
//priority_queue
const ll MAX=1000000;

int Max[MAX+10];
int dp[2][MAX+10];
int a[MAX+10];
int main()
{
    int n,m;
    while(cin>>m>>n)
    {
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        mem(Max,0);
        mem(dp,0);
        int M;
        for(int i=1; i<=m; i++)
        {
            M=-0x3f3f3f3f;
            for(int j=i; j<=n; j++)
            {
                M=max(M,dp[(i-1)%2][j-1]);
                if(i!=j)
                    dp[i%2][j]=max(dp[i%2][j-1]+a[j],M+a[j]);
                else dp[i%2][j]=M+a[j];
            }

        }
        M=-0x3f3f3f3f;
        for(int i=m; i<=n; i++)
            M=max(M,dp[m%2][i]);
        cout<<M<<endl;
    }

}

Posted by yacaph on Fri, 25 Oct 2019 12:42:27 -0700