hdu 1024 最大M子段和

Max Sum Plus Plus

Time Limit: 2000/1000 MS (Java/Others)        Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4521        Accepted Submission(s): 1476

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 (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ Sx ≤ 32767). We define a function sum(i, j) = Si + ... + Sj (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(i1, j1) + sum(i2, j2) + sum(i3, j3) + ... + sum(im, jm) maximal (ix ≤ iy ≤ jx or ix ≤ jy ≤ jx 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)(1 ≤ x ≤ m) 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.
 

Author
JGShining(极光炫影)
 
 
思路:
dp[i][j]表示前j个元素分成i段的最优解,同时这个最优解是由a[j]元素结束的。
转移方程是dp[i][j]=max{f[i][j-1]+a[j],f[i-1][k]+a[j],(i-1<=k<j)} (i<=j<=n-m+i)
其中j的上下界的确定比较麻烦。现在分别解释上界和下界:
上界:dp[i][j]中,如果j=i-1,意思就是在前面i-1个元素中分成i段,这个是不可能实现的。
下界:如果m=n=4,这时dp[2][4]求出来了,意思是前面的四个元素分成了两段,当是还有两段要分,
           所以求出这个是没有意义的。当然求出来也不会影响结果,只是这样时间复杂度就提高了。
这是其中一个特例的状态转移表 m=4,n=6 ,-1 4 -2 3 -2 3
hdu <wbr>1024 <wbr>最大M子段和
没有填的说明不用算。
很显然dp[i][i]=dp[i-1][i-1]+a[i],
所以对角线上面有:
hdu <wbr>1024 <wbr>最大M子段和
现在演示一下转移过程。如何求下图的框中的元素的值
hdu <wbr>1024 <wbr>最大M子段和
由下面涂色的元素的最大值加上a[3]=-2求的如下图
hdu <wbr>1024 <wbr>最大M子段和

最大的是4,所以4+(-2)=2;框中填2;
假如框中的元素是dp[i][j],画圈的元素表示的是,左边那个是dp[i][j-1],上面的几个是dp[i-1][k](i-1<=k<j)} ,这个就是上面的转移方程的表格表示法。
 
 
其他细节如果理解上面的内容,就可以优化了
 
原作者代码:
#include<stdio.h>
__int64 dp[2][1000001];
__int64 a[1000001];
__int64 b[1000001];
__int64 res;
int n,m;
__int64 Max(__int64 x,__int64 y)
{
    if(x>y)return x;
    else   return y;
}
int main()
{
   
//    freopen("a.txt","r",stdin);
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        int t=1;
        res=0;
        int i,j,k;
        for(i=1;i<=n;i++){b[i]=0;dp[0][i]=dp[1][i]=0;scanf("%I64d",&a[i]);}
        for(i=1;i<=m;i++)
        {
            dp[t][i]=dp[1-t][i-1]+a[i];
            __int64 max=dp[1-t][i-1];
            for(j=i+1;j<=n-m+i;j++)
            {
                max=Max(max,dp[1-t][j-1]);
                dp[t][j]=Max(dp[t][j-1],max)+a[j];
            }
            t=1-t;
        }
        t=1-t;
        res=-1111111111111;
        for(j=m;j<=n;j++)if(res<dp[t][j])res=dp[t][j];
        printf("%I64d\n",res);
    }

    return 0;
}

自己想了下,自己的的代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 1000008

using namespace std;

__int64 dp[2][maxn];
int a[maxn];

int main() {
    int n, m, i, j;
    while(~scanf("%d%d", &m, &n)) {
        for(i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        for(i = 0; i <= n; i++)
            dp[0][i] = 0;
            int v = 0;
        for(i = 1; i <=m; i++) {    //枚举m
            for(j = 0; j <= n; j++)  //滚动数组归零
                dp[v^1][j] = 0;
            __int64 mm = -1000000001;
            for(j = i; j <= n-m+i; j++) {  //要循环n-m+1次,并且从 i开始
                if(mm < dp[v][j-1])     
                    mm = dp[v][j-1];  // 保证mm为最大值

                if(mm < dp[v^1][j-1] && j != i)  //这里注意j不能等于i,自己想下为什么,根据上面的解析;
                    dp[v^1][j] = dp[v^1][j-1] + a[j];
                else
                    dp[v^1][j] = mm + a[j];
            }
            v ^= 1;
        }
        __int64 t = -1000000001;
        for(i = m; i <= n; i++) {    //找出分m个之后的最大值
            if(t < dp[v][i])
                t = dp[v][i];
        }
        cout<<t<<endl;
    }
    return 0;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值