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. Git 常用操作(一)

    使用git pull文件时和本地文件冲突: $ git stash $ git pull $ git stash pop stash@{0}   [还原暂存的内容] 上传项目流程: pwd git p ...

  2. Hdu3579 Hello Kiki

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

  3. MFC中用正则表达式进行有效性验证

    转载自:http://blog.csdn.net/jinhill/article/details/5928993 正则表达式最实用的一个地方是验证用户输入.它可以轻松验证邮编.电话号码.信用卡号码-- ...

  4. Eclipse 调试

    F6:(Step Over)单步执行每一行程序: F8:(Resume)继续执行该程序直到下一个断点或程序结束: F5: (Step Into)跳入一个方法内部: F7:(Step Return)从当 ...

  5. HDU 5643 King's Game | 约瑟夫环变形

    经典约瑟夫环 }; ; i<=n; i++) { f[i] = (f[i-] + k) % i; } 变形:k是变化的 #include <iostream> #include &l ...

  6. 企业CEO最核心的应该是销售意识

    一个企业的本质是赚利润,利润怎么来?靠卖东西,所以企业里面最重要的应该是销售人员.在一些500强的外企里有一个规定,没有做过销售的人是很难升到总经理的,在以色列的军队中,没有当过班长,是不可以被提拔为 ...

  7. 【NOI】2004 郁闷的出纳员

    [算法]平衡树(treap) [题解] treap知识见数据结构. 解法,具体细节见程序. #include<cstdio> #include<algorithm> #incl ...

  8. vs调试 配置IISExpress允许局域网内部访问

    内网可访问后,本机不能使用localhost   1.找到IISExpress的配置文件,位于 <文档>/IISExpress/config文件夹下,打开applicationhost.c ...

  9. javascript 变量类型判断

    一.typeof 操作符 对于Function, String, Number ,Undefined 等几种类型的对象来说,他完全可以胜任,但是为Array时 "); typeof arr ...

  10. 从零开始PHP攻略(000)——关于WAMPServer集成环境

    Apache.PHP和MySQL都可以用于多种操作系统和Web服务器的组合.本篇介绍在Windows下用WampServer环境包来搭建本地php环境. W:windows A:Apache M:My ...