关闭

hdoj 1024 Max Sum Plus Plus 【简单dp】

89人阅读 评论(0) 收藏 举报
分类:


Max Sum Plus Plus

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


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
分析:
最大区域子段和问题
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<stack>
#define mem(x,y) memset(x,y,sizeof(x))
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
#define inf 0x80000000
using namespace std;
const int N=1e6+10;
int pp[N],dp[N],bb[N];
int main()
{
    int n,m,i,j,k,mmax;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=1;i<=m;i++)
            scanf("%d",&pp[i]);
        for(i=0;i<=m;i++)
            dp[i]=0,bb[i]=0;
        for(i=1;i<=n;i++)
        {
            mmax=inf;
            for(j=i;j<=m;j++)
            {
                if(dp[j-1]>bb[j-1])
                    dp[j]=dp[j-1]+pp[j];
                else dp[j]=bb[j-1]+pp[j];
                bb[j-1]=mmax;
                if(mmax<dp[j])
                    mmax=dp[j];
            }
            bb[j-1]=mmax;
            //printf("%d\n",bb[j-1]);
        }
        printf("%d\n",mmax);
    }
    return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:34398次
    • 积分:2258
    • 等级:
    • 排名:第16623名
    • 原创:202篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条