【HDU 1024 Max Sum Plus Plus 】

学习的博客
题目链接
其实就是 dp[i][j] 代表以i结尾(i必须取) 已经有j个子段的最大值
那么dp[i][j] 只能通过 dp[i-1][j] 和 dp[i-1][j-1] 过来
滚动数组优化一维后发现其实和BZOJ 1270有异曲同工之妙
记录一个最大值 然后最大值作为上一个循环的最大值传过来进行直接传递

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N=1e6+5;
int n,m,arr[N];
//int f[N][N];
int dp[N],maxn[N];
int main(){
    while(scanf("%d%d",&m,&n)==2)
    {
        for(int i = 1;i<=n;++i) scanf("%d",&arr[i]);
        memset(dp,0,sizeof(dp));
        memset(maxn,0,sizeof(maxn));
        int nowans = 0;
        for(int i = 1;i<=m;++i)
        {
            nowans = -1e9;
            for(int j = i ;j<=n;++j)
            {
                dp[j] = max(dp[j-1],maxn[j-1]) + arr[j];
                maxn[j-1] = nowans;
                nowans = max(dp[j],nowans);
            }
        }
        printf("%d\n",nowans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/heucodesong/article/details/89324565