hdoj 1024 Max Sum Plus Plus 【简单dp】

原创 2015年11月17日 21:39:55


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;
}

版权声明:博主情人,外人误碰!!!

相关文章推荐

Hdoj 1024 Max Sum Plus Plus 【DP】

Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other...

暑期dp46道(18)--HDOJ 1024 Max Sum Plus Plus

题目链接:HODJ 1024 这题花的时间蛮多的,不过没有浪交,就是之前有几位做过的说这题很“巧”,需要费时间,我才没敢轻易交,大概花了一天时间交了一发,A掉,还多开了内存,尴尬,不过确实挺...
  • ADjky
  • ADjky
  • 2016-08-10 14:21
  • 224

hdoj1024 Max Sum Plus Plus (DP)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意: 给一个数字序列a1,a2,a3,...,an,(n 输入格式为:m  n  a1  a2  ...

HDOJ-1024 Max Sum Plus Plus (最大M子段和问题)

本题的大致意思为给定一个数组,求其分成m个不相交子段和最大值的问题。 设Num为给定数组,n为数组中的元素总数,Status[i][j]表示前i个数在选取第i个数的前提下分成j段的最大值,其中1<=j...

HDOJ 1024 Max Sum Plus Plus -- 动态规划

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Problem Description Now I think you have got ...
  • JDPlus
  • JDPlus
  • 2014-02-26 13:37
  • 1697

hdoj 1024 Max Sum Plus Plus(m个子段最大和)

Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other...

HDU/HDOJ 1024 The Max Sum Plus Plus

是别人写的解题报告,代码自己重新了一遍,但是思路依然不是特别清晰。 http://acm.hdu.edu.cn/showproblem.php?pid=1024 问题:给定由n个整数(...

HDU-1024 Max Sum Plus Plus(DP)

题意:可以认为是最大和子序列的进化版, 要求从一串序列中取出若干段, 使得这几段的和最大。 思路: 可以先推出来一个二维的转移方程式: dp[i][j] = max(dp[i-1][j]+num[i]...
  • yo_bc
  • yo_bc
  • 2017-05-23 11:52
  • 111

【HDU 1024 Max Sum Plus Plus】+ dp

Max Sum Plus PlusTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)...

HDU - 1024 Max Sum Plus Plus(DP)

Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we always cha...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)