Max Sum Plus Plus

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

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
 
没做出来。。只能想到二维的,滚动数组果然厉害.

题意:n个数字分成m段的最大和
分析:dp[i][j] 代表以a[j]结尾的前j个数字被分成 i 段得到的最大和
可以得到: 1.如果a[j]单独成段 dp[i][j] = dp[i-1][k] + a[j] 其中 1<=k<j 意思是前1- k 个数字组成了i-1段.
             2.如果a[j]并入第i段 那么dp[i][j]=dp[i][j-1]+a[j] 分析可得 dp[i][j] = max(1,2)
但是这题的条件是不允许这样做的 ,首先枚举 i , j ,k 的时间复杂度是 O(n^3) dp数组的空间要 O(n^2),而数据量已经到达了 1000000 显然不允许.于是,这里就要用一个新的思想了:滚动数组.
我们可以看到dp[i][j]只和是否包含a[j]相关,所以这里我们可以用两个一维数组存当前状态与前一个状态.
新的状态: dp[j]表示以a[j]结尾的前i段的最大和,pre[j]表示前j个数组成前i段的最大和,不一定包括a[j]
dp[j] = max(dp[j-1]+a[j],pre[j-1]+a[j])

/**题意:n个数字分成m段的最大和*/
///分析:dp[i][j] 代表以a[j]结尾的前j个数字被分成 i 段得到的最大和
///可以得到: 1.如果a[j]单独成段 dp[i][j] = dp[i-1][k] + a[j] 其中 1<=k<j 意思是前1- k 个数字组成了i-1段.
/// 2.如果a[j]并入第i段 那么dp[i][j]=dp[i][j-1]+a[j] 分析可得 dp[i][j] = max(1,2)
///但是这题的条件是不允许这样做的 ,首先枚举 i , j ,k 的时间复杂度是 O(n^3) dp数组的空间要 O(n^2),而数据量已经
///到达了 1000000 显然不允许.于是,这里就要用一个新的思想了:滚动数组.
///我们可以看到dp[i][j]只和是否包含a[j]相关,所以这里我们可以用两个一维数组存当前状态与前一个状态.
///新的状态: dp[j]表示以a[j]结尾的前i段的最大和,pre[j]表示前j个数组成前i段的最大和,不一定包括a[j]
///dp[j] = max(dp[j-1]+a[j],pre[j-1]+a[j])
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int N = ;
int a[N];
int dp[N];
int pre[N];
int main()
{
int m,n;
while(scanf("%d%d",&m,&n)!=EOF){
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
dp[i]=pre[i]=;
}
dp[]=pre[]=;
int mam;
for(int i=;i<=m;i++) {///枚举每一段
mam = -0x7fffffff;
for(int j=i;j<=n;j++){
dp[j] = max(dp[j-]+a[j],pre[j-]+a[j]);
pre[j-] = mam; ///表示前 j-1 个数组成i段能够表示的最大和
mam=max(dp[j],mam);
}
}
printf("%d\n",mam);
}
return ;
}

hdu 1024(滚动数组+动态规划)的更多相关文章

  1. hdu 1513(滚动数组)

    Palindrome Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. HDU - 3033 滚动数组有坑

    每层至少一个,滚动时要判上一层非法与否,所以每次都要memset #include<bits/stdc++.h> #define rep(i,j,k) for(int i=j;i<= ...

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

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  4. HDU 1024 A - Max Sum Plus Plus DP + 滚动数组

    http://acm.hdu.edu.cn/showproblem.php?pid=1024 刚开始的时候没看懂题目,以为一定要把那n个数字分成m对,然后求m对中和值最大的那对 但是不是,题目说的只是 ...

  5. hdu 1024 dp滚动数组

    #include <cstdio> #include <iostream> #include <algorithm> #include <queue> ...

  6. HDU - 1024 Max Sum Plus Plus 最大m段子段和+滚动数组优化

    给定n个数字,求其中m段的最大值(段与段之间不用连续,但是一段中要连续) 例如:2 5 1 -2 2 3 -1五个数字中选2个,选择1和2 3这两段. dp[i][j]从前j个数字中选择i段,然后根据 ...

  7. HDU 1024 Max Sum Plus Plus (动态规划)

    HDU 1024 Max Sum Plus Plus (动态规划) Description Now I think you have got an AC in Ignatius.L's "M ...

  8. hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)

    题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...

  9. 动态规划+滚动数组 -- POJ 1159 Palindrome

    给一字符串,问最少加几个字符能够让它成为回文串. 比方 Ab3bd 最少须要两个字符能够成为回文串 dAb3bAd 思路: 动态规划 DP[i][j] 意味着从 i 到 j 这段字符变为回文串最少要几 ...

随机推荐

  1. Poco::Path 和 DirectoryIterator

    #include<iostream>#include<typeinfo>#include<Poco/Path.h>using namespace std;using ...

  2. 2017-7-19-每日博客-关于Linux下的CentOS中文件夹基本操作命令.doc

    CentOS中文件夹基本操作命令 文件(夹)查看类命令 ls--显示指定目录下内容 说明:ls 显示结果以不同的颜色来区分文件类别.蓝色代表目录,灰色代表普通文件,绿色代表可执行文件,红色代表压缩文件 ...

  3. tinyxml源码解析(中)

    转载于:http://www.cnblogs.com/marchtea/archive/2012/11/20/2766756.html 前言: 之前趁着这段时间比较空闲,也因为听闻tinyxml大名, ...

  4. linux 命令后台运行(转载)

    原文连接:https://www.cnblogs.com/lwm-1988/archive/2011/08/20/2147299.html 有两种方式: 1. command & : 后台运行 ...

  5. SqlServer中循环和条件语句示例!

    -- ╔════════╗ -- =============================== ║ if语句使用示例 ║ -- ╚════════╝ declare @a int set @a=12 ...

  6. 理解JavaScript的prototype和__proto__

    首先,要明确几个点: 1.在JS里,万物皆对象. 方法(Function)是对象,方法的原型(Function.prototype)是对象.因此,它们都会具有对象共有的特点.即:对象具有属性__pro ...

  7. 【C++对象模型】第一章 关于对象

    1.C/C++区别 C++较之C的最大区别,无疑在于面向对象,C程序中程序性地使用全局数据.而C++采用ADT(abstract data tpye)或class hierarchy的数据封装.类相较 ...

  8. Jmeter-分布式

    转载自: http://www.51testing.com/html/28/116228-247521.html 由于Jmeter本身的瓶颈,当需要模拟数以千计的并发用户时,使用单台机器模拟所有的并发 ...

  9. 「6月雅礼集训 2017 Day11」tree

    [题目大意] 给出一棵带权树,有两类点,一类黑点,一类白点. 求切断黑点和白点间路径的最小代价. $n \leq 10^5$ [题解] 直接最小割能过..但是树形dp明显更好写 设$f_{x,0/1/ ...

  10. 【51NOD】数据流中的算法

    [算法]数学 [题解] 1.平均数:累加前缀和.//听说要向下取整? 2.中位数:双堆法,大于中位数存入小顶堆,小于中位数存入大顶堆,保证小顶堆内数字数量≥大顶堆,奇数则取小堆顶,偶数则取两堆顶/2. ...