[Base] hdu-1024 DP

Topic Link

Title Description

To n number, the n number of zoned m para find zoned m maximum segment does not need to go all numbers are designated.

Thinking

Because it is seeking to n number designated as m the maximum number of segments, so that the establishment of the state of DP [i] [j] denotes the front j number of the designated i maximum segment.
There are two state transition equation:
DP [I] [j-. 1] + A [j] (represented by the first j number incorporated in the preceding paragraph)
DP [-I. 1] [j-. 1] + A [j ] (j indicates the number of independent first let into sections)
as the subject to relatively small space, so it is necessary to compress the state
f [j-1] it means that DP [-I. 1] [j-. 1] , i.e. before j- a number of the i 1- maximum segments

Code

//hdu-1024
#include<bits/stdc++.h>
using namespace std;

const int inf=0x3f3f3f3f;
const int N=1e6+10;
int a[N];
int dp[N];
int f[N];

void solve(int m, int n)
{
    for(int i=1; i<=n; i++) {
        scanf("%d",&a[i]);
        dp[i]=f[i]=0;
    }
    int mx;
    for(int i=1; i<=m; i++) {
        mx=-inf;
        for(int j=i; j<=n; j++) {
            dp[j]=max(dp[j-1],f[j-1])+a[j];
            f[j-1]=mx;		//每次更新f[j-1],为下一次i的增加使用做准备
            mx=max(mx,dp[j]);
        }
    }
    printf("%d\n",mx);
}

int main()
{
    //freopen("in.txt","r",stdin);
    int m,n;
    while(~scanf("%d %d",&m,&n)) {
        solve(m,n);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/ZX-GO/p/12569049.html