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. HDU3579 线性同余方程(模板 余数不一定互质)

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  2. Hive分组后取组内排名方法row_number

    今天遇到这样一个需求场景,要取出 每一种分类(a,b组合分类) 符合条件的日期(字段c) 距离现在最近的10个日期 的数据 首先想到的是用sql筛选出符合某种条件的所有数据,这样的事情很简单 然后用脚 ...

  3. HTML或者JSP页面--执行完某事件后刷新页面,重置表单,清空数据

    在提交表单或者执行某个事件之后,如果需要重置表单(即清空表单里的数据) 可以执行下面代码来完成 方式一: self.location.href="userController.do?goAd ...

  4. Java进行http请求工具类代码(支持https)

    package com.guyezhai.modules.utils; import java.io.BufferedReader; import java.io.DataOutputStream; ...

  5. bzoj3716/4251 [PA2014]Muzeum

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3716 http://www.lydsy.com/JudgeOnline/problem.ph ...

  6. ios应用里面进入app store 下载界面

    转自:http://blog.csdn.net/diyagoanyhacker/article/details/6654838 在IOS应用里直接打开app store 评论页面的方法: [[UIAp ...

  7. 联系博主 Contact

    李莫 / Ray OI 蒟蒻一只 / A Player of Olympiad in Informatics QQ:740929894 邮箱 / Email :rayking2017@outlook. ...

  8. js_实现给未来元素添加事件。

    未来元素:不是一个页面上的元素,是通过js或者通过后台直接渲染在页面上的元素,也就是说这些元素不是直接写在document中的. 1.对于未来元素,我们想直接用js或者jq操作它们是不起作用的. $( ...

  9. hdu 1599 find the mincost route (最小环与floyd算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1599 find the mincost route Time Limit: 1000/2000 MS ...

  10. Angular2.0 基础: Form

    对于Angular2.0 的Form表单中的隐藏和验证,个人觉得还是挺有意思的. 1.通过ngModel 跟踪修改状态与验证. 在表单中使用 ngModel 可以获得更多的控制权,包括一些常用的验证. ...