HDU 1024 最大m个子段和滚动数组

来源:互联网 发布:唐安琪烧伤真相知乎 编辑:程序博客网 时间:2024/05/28 19:23

Max Sum Plus Plus

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


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 32 6 -1 4 -2 3 -2 3
 

Sample Output
68
Hint
Huge input, scanf and dynamic programming is recommended.题意:让求从n个数中找出m个子段 的最大和,(每个子段自己是连续的,每个子段之间没有重复元素)

设输入的数组为a[1...n],从中找出m个段,使者几个段的和为最大

dp[i][j]表示前j个数中取i个段的和的最大值,其中最后一个段包含a[j]。

则状态转移方程为:

dp[i][j]=max{dp[i][j-1]+a[j],max{dp[i-1][t]}+a[j]}    i-1=<t<j-1

 

因为dp[i][j]中a[j]可能就自身一个数组成最后一段,或者a[j]与a[j-1]等前面的数组成最后一段。


此题n数据太大,二维数组开不下,而且三重循环,想到状态转移方程后还是困难重重。

想想,二维数组不行的话,肯定要压缩成一维数组:

因为dp[i-1][t]的值只在计算dp[i][j]的时候用到,那么没有必要保存所有的dp[i][j] for i=1 to m,这样我们可以用一维数组存储。

用pre[j]表示j之前一个状态dp[i-1][]中1-j之间,不一定包含a[j]的最大字段和,然后推dp[i][j]状态时,dp[i][j]=max{pre[j-1],dp[j-1]}+a[j];

褐色的为了方便理解,其实不存在。





4 6
2 -4 5 6 -8 10
dp[1]=2 pre[0]=-100000000
dp[2]=-2 pre[1]=2
dp[3]=5 pre[2]=2
dp[4]=11 pre[3]=5
dp[5]=3 pre[4]=11
dp[6]=13 pre[5]=11
dp[2]=-2 pre[1]=-100000000
dp[3]=7 pre[2]=-2
dp[4]=13 pre[3]=7
dp[5]=5 pre[4]=13
dp[6]=21 pre[5]=13
dp[3]=3 pre[2]=-100000000
dp[4]=13 pre[3]=3
dp[5]=5 pre[4]=13
dp[6]=23 pre[5]=13
dp[4]=9 pre[3]=-100000000
dp[5]=5 pre[4]=9
dp[6]=23 pre[5]=9
23

dp[j]不一定是该行最大的
dp[j]表示将a[j]加上的最大值

#include<bits/stdc++.h>using namespace std;int dp[1000010],a[1000010];int M[1000010]; //全部存的是 i-1段的最大值int main(){    int n,m;    while(~scanf("%d%d",&m,&n))    {        for(int i=1;i<=n;i++)            scanf("%d",&a[i]);        memset(dp,0,sizeof(dp));        memset(M,0,sizeof(M));        int Max=-100000000;        for(int i=1;i<=m;i++)        {           Max=-100000000;           for(int j=i;j<=n;j++)      //必须从i开始,否则上一轮的最大值会被覆盖掉           {    //dp[j]的取值是从 1在第i个子段后面加上a[j]   2是让a[j]成为第i段的第一个元素(独立成为yi段)               dp[j]=max(dp[j-1]+a[j],M[j-1]+a[j]);               M[j-1]=Max;            //用于存放在 j-1个数字中 i-1个子段和的最大值//printf("dp[%d]=%d pre[%d]=%d\n",j,dp[j],j-1,pre[j-1]);              Max=max(Max,dp[j]);           }        }        printf("%d\n",Max);    }    return 0;}

 二维思路
 while(~scanf("%d%d",&m,&n))    {        for(int i=1;i<=n;i++)            scanf("%d",&a[i]);        memset(dp,0,sizeof(dp));        memset(M,0,sizeof(M));        int Max=-100000000;       for(int i=1;i<=m;i++)        {          // Max=-100000000;           for(int j=i;j<=n;j++)           {    //dp[j]的取值是从 1在第i个子段后面加上a[j]   2是让a[j]成为第i段的第一个元素(独立成为yi段)               if(j>i)               {                dp[i][j]=dp[i][j-1]+a[j];                for(int k=i-1;k<=j-1;k++)                dp[i][j]=max(dp[i][j],dp[i-1][k]+a[j]);               }               else                dp[i][j]=dp[i-1][j-1]+a[j];           }        }        for(int i=m;i<=n;i++)        printf("%d\n",dp[m][i]);    }


1 0
原创粉丝点击