题目链接:http://acm.split.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): 31798    Accepted Submission(s): 11278

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(极光炫影)
 
 
 
题解:
1.设last_max[i][j]为:处理到第j个数时,第j个数属于第i个序列的最大值。last_max[i][j]必定包含a[j]。
2.设all_max[i][j]为 :处理到第j个数时, 分成i个序列的最大值。all_max[i][j]不一定包含a[j], 其值实际上是 max( last_max[i][k] )   其中i<=k<=j,all_max数组的实际作用是:当a[j]独立出来自己作为一个序列时,找到前面i-1个序列和的最大值, 以便与a[j]拼接成i个序列。
3.状态转移:last_max[i][j] = max( last_max[i][j-1],  all_max[i-1][j-1] ) + a[j]。即以a[j]是否独立出来自己作为一个序列来讨论。
4.观察状态转移方程, last_max数组不需要知道上一步的信息,所以只需要开一维;all_max数组只需要知道上一步的信息,所以可以用滚动数组以节省空间。
 
 
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e6+; int a[MAXN];
int last_max[MAXN], all_max[][MAXN];
int main()
{
int n, m;
while(scanf("%d%d",&m, &n)!=EOF)
{
for(int i = ; i<=n; i++)
scanf("%d", &a[i]); ms(all_max, );
ms(last_max, ); for(int i = ; i<=m; i++)
{
last_max[i] = all_max[i&][i] = last_max[i-]+a[i];
for(int j = i+; j<=n; j++)
{
last_max[j] = max(last_max[j-], all_max[!(i&)][j-]) + a[j];
all_max[i&][j] = max(all_max[i&][j-], last_max[j]);
/**以上代码的实际意义为:
last_max[i][j] = max(last_max[i][j-1], all_max[i-1][j-1]) + a[j];
all_max[i][j] = max(all_max[i][j-1], last_max[i][j]);
**/
}
}
printf("%d\n", all_max[m&][n]);
}
return ;
}

HDU1024 Max Sum Plus Plus —— DP + 滚动数组的更多相关文章

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

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

  2. HDU-1024 Max Sum Plus Plus 动态规划 滚动数组和转移优化

    题目链接:https://cn.vjudge.net/problem/HDU-1024 题意 给n, m和一个序列,找m个不重叠子串,使这几个子串内元素和的和最大. n<=1e6 例:1 3 1 ...

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

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

  4. hdu Max Sum Plus Plus(dp+滚动数组)

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 m为段,要深刻理解题意,并没有说是段与段要连接. 题解链接:http://blog.csdn.n ...

  5. HUD 1024 Max Sum Plus Plus (滚动数组)

    题意:从一个序列中选出分成不交叉的m段 的最大和 解析 : 题目中 1 <= n <=1000000 所以二维数组是不能用了  所以 要想到简化为一维 dp[i][j]表示以i结尾的前i个 ...

  6. HDU1024 Max Sum Plus Plus(DP)

    状态:d(i,j)它代表前j划分数i部并且包括第一j最佳结果时的数.g(i,j)表示前j划分数i最好的结果时,段,g(m,n)结果,需要. 本题数据较大.需採用滚动数组.注意:这题int类型就够用了, ...

  7. HDU 5119 Happy Matt Friends (背包DP + 滚动数组)

    题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt ...

  8. POJ 3666 Making the Grade (DP滚动数组)

    题意:农夫约翰想修一条尽量平缓的路,路的每一段海拔是A[i],修理后是B[i],花费|A[i] – B[i]|,求最小花费.(数据有问题,代码只是单调递增的情况) #include <stdio ...

  9. USACO 2009 Open Grazing2 /// DP+滚动数组oj26223

    题目大意: 输入n,s:n头牛 s个栅栏 输入n头牛的初始位置 改变他们的位置,满足 1.第一头与最后一头的距离尽量大 2.相邻两头牛之间的距离尽量满足 d=(s-1)/(n-1),偏差不超过1 3. ...

随机推荐

  1. Oracle の ty_str_split + MySQL の proc_split

    oracle实现字符串分割 功能描述:用指定分隔符切割输入的字符串,返回一维数组,每个数组元素为一个子串. ); CREATE OR REPLACE FUNCTION fn_split (p_str ...

  2. Apache手册

    一.apache的安装 如果不指定安装位置,默认为/usr/local/apache2/

  3. loadrunner协议开发

    可以参考loadrunner自带的VuGen Guild文档,里面详细描述了所有协议的录制和开发内容

  4. HDU 5421 Victor and String

    Victor and String Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on HDU. Orig ...

  5. NIUDAY 11.23 北京站抢票啦 | 看 AI 落地行业 享 AI 时代红利

    2018 年是见证「奇迹」的一年.AI 从多年的热门话题中开始走下神坛,逐渐深入到了各个行业,加速经济结构优化及行业智慧化升级,AI 已不再是难以企及的神话而是可触摸的美好未来. 政策支持加上资本推动 ...

  6. CSU 1605 数独

    题目大意: 9宫格每个位置都有对应的分数,填完数独后根据对应位置的分数相加之和求个最大值,不存在输出-1 说什么用位运算加速可以解决问题,但是对着标程还是T,最近学了dlx,发现这样解决数独快了很多 ...

  7. [luoguP2948] [USACO09OPEN]滑雪课Ski Lessons(DP)

    传送门 f[i][j]表示i时刻能力值为j的最大滑雪数 显然f[0][1]=0,开始搜索 三种转移: ①美美的喝上一杯**:f[i+1][j]=max(f[i+1][j],f[i][j]) ②滑雪,f ...

  8. poj1091:跳蚤【容斥原理】

    题目大意:中文题就不翻译了 思路:假设跳蚤选择X1个第一张卡片,X2个第二张卡片...Xn个第n张卡片,Xn+1张写着m的卡片,那么就可以列出方程:a1*X1+a2*X2+…+an*Xn+m*X(n+ ...

  9. 【01背包变形】Robberies HDU 2955

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 [题意] 有一个强盗要去几个银行偷盗,他既想多抢点钱,又想尽量不被抓到.已知各个银行 的金钱数和被抓的概率 ...

  10. VK Cup 2015 - Qualification Round 1 A. Reposts [ dp DAG上最长路 ]

    传送门 A. Reposts time limit per test 1 second memory limit per test 256 megabytes input standard input ...