HDU 1024 Max Sum Plus Plus (递推)

Max Sum Plus Plus

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

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.

dp[i][j][0] ... 表示前i个数分成j个组,不选第i个数的最大得分

dp[i][j][1] ... 表示前i个数分成j个组,选第i个数的最大得分

因为状态i只跟状态i-1, 所以可以用滚动数组来减空间

取最要自己写 。 否则卡常数会超时

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std ;
const int N = 100010;
const int inf = 1e9+7;

int dp[2][N][2] , n , m , x[N] ;
inline int MAX( int a , int b ) {
    if( a > b ) return a ;
    else return b ;
}
int main() {
//    freopen("in.txt","r",stdin);
    while( ~scanf("%d%d",&m,&n) ) {
        for( int i = 1 ; i <= n ; ++i ) {
            scanf("%d",&x[i]);
        }
        int v = 0 ;
        dp[v][0][0] = 0 ;
        dp[v][1][1] = x[1] ;
        for( int i = 1 ; i < n ; ++i ) {
            for( int j = 0 ; j <= i + 1 && j <= m ; j++ ) {
                dp[v^1][j][0] = dp[v^1][j][1] = -inf ;
            }
            for( int j = min( m , i ) ; j >= 0 ; --j ) {
                if( j != i ) {
                    dp[v^1][j+1][1] = MAX( dp[v][j][0] + x[i+1] , dp[v^1][j+1][1] );
                    dp[v^1][j][0] = MAX( dp[v][j][0] , dp[v^1][j][0]);
                }
                if( j != 0 ) {
                    dp[v^1][j][1] =  MAX ( dp[v^1][j][1] , dp[v][j][1] + x[i+1] ) ;
                    dp[v^1][j+1][1] = MAX ( dp[v^1][j+1][1] , dp[v][j][1] + x[i+1] ) ;
                    dp[v^1][j][0] = MAX ( dp[v^1][j][0] , dp[v][j][1] ) ;
                }
            }
            v ^= 1 ;
        }
        int ans = -inf ;
        if( m < n ) ans = MAX( ans , dp[v][m][0] );
        if( m > 0 ) ans = MAX( ans , dp[v][m][1] );
        printf("%d\n",ans);
    }
    return 0 ;
}

时间: 2024-10-18 03:19:06

HDU 1024 Max Sum Plus Plus (递推)的相关文章

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]直接接在前面

[2016-03-28][HDU][1024][Max Sum Plus Plus]

时间:2016-03-28 17:45:33 星期一 题目编号:[2016-03-28][HDU][1024][Max Sum Plus Plus] 题目大意:从n个数字提取出一定数字组成m个部分,使得这个部分的总和最大 分析: dp[i][j]表示前i段计算第j个数字,dp[i][j] = max(dp[i - 1][j - 1] + a[j],dp[i][k] + a[j]); #include <algorithm> #include <cstring> #include &

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的数组肯定会爆炸,所以要对方程

hdu 1024 Max Sum Plus Plus(DP)

转移方程dp[i][j]=Max(dp[i][j-1]+a[j],max(dp[i-1][k] ) + a[j] ) 0<k<j 此链接中有详解点击打开链接 #include<stdio.h> #include<algorithm> #include<iostream> using namespace std; #define MAXN 1000000 #define INF 0x7fffffff int dp[MAXN+10]; int mmax[MAXN

HDU 1024 Max Sum Plus Plus Dp题解

本题就是求m段子段,而且要求这些子段加起来和最大,最大子段和的Plus版本. 不过题意真的不好理解,x,y什么的都没有说清楚. 知道题意就开始解题了,这肯定是动态规划法了. 动态规划法的程序不难写,关键是抽象思维. 这里的最小情况是只分成一段的时候,就退化为最大子段和问题了,这个是段数的最小情况了: 如果只有0个数的时候,结果肯定为零了,或者如果只有一个数的时候就是这个数了,那么数列只有0个或者1个的时候就是数组的最小情况了. 然后记录使用一个数组记录dp[MAX_N],其中dp[i]的含义就是

HDU 1024 Max Sum Plus Plus

题目链接: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): 21926    Accepted Submission(s): 7342 Problem Description Now I think you ha

HDU 1024 Max Sum Plus Plus【动态规划求最大M子段和详解 】

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

hdu 1024 Max Sum Plus Plus(DP&amp;最大连续和加强版)

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

HDU 1024 Max Sum Plus Plus 动态规划

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题目大意:n个数分成两两不相交的m段,求使这m段和的最大值. 解题思路:比较坑的点:n2 能过:long long超时,int AC. dp[i][j]:= 在选择第i个数的情况下前i个数分成j段的最大值dp[i][j] = max(dp[i - 1][j] + a[i], max(dp[x][j - 1] -> dp[x][j - 1]) + a[i]) x < i 由于n<1000