HDU1024——DP——Max Sum Plus Plus

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(极光炫影)

Recommend

We have carefully selected several similar problems for you:  1074 1025 1081 1080 1160

大意:主要是降低复杂度,问给你m个数要可以分成n块连续的数,问最大和是多少

定义dp[i][j]表示当前这个数为i,已经分了j块

可以转来的方向  1.块数不动多一个数   2.块数动了多一个数

动态转移方程  dp[i][j] = max(dp[i-1][j]+a[i],max(dp[1][j-1].....dp[i-1][j-1])+a[i]))

bing神代码好流弊。。定义一个数组记录前面一个块的前j-1中最大的值

那么最后答案就是这个max2(分成n组时1到j中最大的值

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
int dp[maxn];
int a[maxn],max1[maxn];
int main()
{
    int n,m;
    int max2;
    while(~scanf("%d%d",&n,&m)){
        for(int i = 1; i <= m ;i++)
            scanf("%d",&a[i]);
        memset(dp,0,sizeof(dp));
        memset(max1,0,sizeof(max1));
        for(int i = 1; i <= n; i++){
             max2 = -inf;
            for(int j = i; j <= m; j++){
                   dp[j] = max(dp[j-1]+a[j],max1[j-1]+a[j]);
                   max1[j-1] = max2;
                   max2 = max(max2,dp[j]);
            }
        }
        printf("%d\n",max2);
    }
    return 0;
}

  

时间: 2024-10-08 06:31:38

HDU1024——DP——Max Sum Plus Plus的相关文章

HDU1003——DP——Max Sum

Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input contains

[VJ][DP]Max Sum Plus Plus

Max Sum Plus Plus 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 n

HDU 1024 DP Max Sum Plus Plus

题意:本题的大致意思为给定一个数组,求其分成m个不相交子段和最大值的问题. kuangbin专题. dp[i][j]=Max(dp[i][j-1]+a[j] , max( dp[i-1][k] ) + a[j] ) 0<k<j 注意可以换成一维数组.第一维i没有影响,我们要求dp[m][n],所以i维可以省去,每次保留前j-1个数之中最大的部分. 1 #include<iostream> 2 #include<string> 3 #include<algorith

hdu1024 Max Sum Plus Plus (Dp)

hdu1024 Max Sum Plus Plus 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 numbe

HDU1024 Max Sum Plus Plus 【DP】

Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17164    Accepted Submission(s): 5651 Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem

HDU 1024 Max Sum Plus Plus --- dp+滚动数组

HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值,其中第i个子序列包括a[j], 则max(dp[m][k]),m<=k<=n 即为所求的结果 <2>初始状态: dp[i][0] = 0, dp[0][j] = 0; <3>状态转移: 决策:a[j]自己成为一个子段,还是接在前面一个子段的后面 方程: a[j]直接接在前面

Hdoj 1024 Max Sum Plus Plus 【DP】

Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18861 Accepted Submission(s): 6205 Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b

HDU 1024:Max Sum Plus Plus(DP)

http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24675    Accepted Submission(s): 8478 Problem Description Now I think you have g

Hdu 1024 Max Sum Plus Plus (dp)

题目链接: Hdu 1024 Max Sum Plus Plus 题目描述: 给出n个数,问m段连续子序列的和相加最大是多少? 解题思路: dp[i][j]表示把前i个元素(包括第i个),分成j段的最大和.状态转移方程就是dp[i][j] = max (dp[i-1][j] + arr[j],  max( dp[k][j-1]) + arr[j]),其中0<k<i.(第i个元素是保存在第j段,还是自己单独成段) 由于1<=n<=1000,000.n*n的数组肯定会爆炸,所以要对方程