题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1024



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
 
Recommend
We have carefully selected several similar problems for you:  1074 1025 1081 1080 1160



分析

设状态为 cur[i,j],表示前 j 项分为 i 段的最大和,且第 i 段必须包含 data[j],则状态转移方程如下:

cur[i,j] = max{cur[i,j − 1] + data[j],max{cur[i − 1,t] + data[j]}}, 其中i ≤ j ≤ n,i − 1 ≤ t < j

target = max{cur[m,j]}, 其中m ≤ j ≤ n


分为两种情况:

• 情况一,data[j] 包含在第 i 段之中,cur[i,j − 1] + data[j]。

• 情况二,data[j] 独立划分成为一段,max{cur[i − 1,t] + data[j]}。

观察上述两种情况可知 cur[i,j] 的值只和 cur[i,j-1] 和 cur[i-1,t] 这两个值相关,因此不需要二维数组,

可以用滚动数组,只需要两个一维数组,用 cur[j] 表示现阶段的最大值,即 cur[i,j − 1] + data[j],用

pre[j] 表示上一阶段的最大值,即 max{cur[i − 1,t] + data[j]}。

#include <stdio.h>
#include <stdlib.h>
#include <limits.h> int MaxSum(int * data, int m, int n){
int i, j, max_sum;
int * cur = (int *)calloc(n + 1, sizeof(int));
int * pre = (int *)calloc(n + 1, sizeof(int));
data = data - 1; //data下标从0开始, cur、pre下标从1开始,为使下标一致,data减1
for (i = 1; i <= m; ++i){
max_sum = INT_MIN;
for (j = i; j <= n; ++j){
if (cur[j - 1] < pre[j - 1])
cur[j] = pre[j - 1] + data[j];
else
cur[j] = cur[j - 1] + data[j];
pre[j - 1] = max_sum;
if (max_sum < cur[j])
max_sum = cur[j];
}
pre[j - 1] = max_sum;
}
free(cur);
free(pre);
return max_sum;
} int main(void){
int m, n, i, *data;
while (scanf("%d%d", &m, &n) != EOF){
data = (int *)malloc(sizeof(int) * n);
for (i=0; i<n; ++i){
scanf("%d", &data[i]);
}
printf ("%d\n", MaxSum(data, m, n));
free(data);
} return 0;
}

参考资料:ACM Cheat Sheet

HDOJ 1024 Max Sum Plus Plus -- 动态规划的更多相关文章

  1. HDU 1024 Max Sum Plus Plus [动态规划+m子段和的最大值]

    Max Sum Plus Plus Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  2. hdu 1024 Max Sum Plus Plus (动态规划)

    Max Sum Plus PlusTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. HDU 1024 Max Sum Plus Plus (动态规划 最大M字段和)

    Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...

  4. 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 ...

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

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

  6. hdu1003 1024 Max Sum&Max Sum Plus Plus【基础dp】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4302208.html   ---by 墨染之樱花 dp是竞赛中常见的问题,也是我的弱项orz, ...

  7. HDU 1024 Max Sum Plus Plus(m个子段的最大子段和)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/ ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. iOS开发-自动布局和自动旋转

    今天学习自动布局中的自动调整尺寸大小. 一.尺寸分类 尺寸分类是对设备宽高的一种大致分类. 有两种具体的尺寸分类用来表示真机:紧凑(Compact)和标准(Regular).还有第三种分类可以在设计工 ...

  2. Android 编程下 java.lang.NoClassDefFoundError: cn.jpush.android.api.JPushInterface 报错

    使用了极光推送的 jar 包项目在从 SVN 中检出后,假设不又一次对 jar 包和 Bulid Path 进行配置就会抛出 java.lang.NoClassDefFoundError: cn.jp ...

  3. 画表格防OFFICE的功能

    http://files.cnblogs.com/xe2011/officetable.rar 画表格防OFFICE的功能

  4. Mac联网恢复系统重新安装Lion

    Mac的Lion系统,虽然不像Windows那样需要经常重装,但也难免会有要重置的时候,比如更换硬盘.本文介绍如何利用Mac的联网恢复系统进行Lion系统的在线恢复.Mac的在线恢复系统只在近几年的机 ...

  5. javascript 引擎Rhino源代码分析 浅析 实例函数对象及this

    http://blog.csdn.net/liantian_wu/article/details/49797481

  6. C#_ajax_demo

    使用asp.net mvc 调用Action方法很简单. 一.无参数方法. 1.首先,引入jquery-1.5.1.min.js 脚本,根据版本不同大家自行选择. <script src=&qu ...

  7. Java基础知识强化之网络编程笔记14:TCP之多个客户端上传到一个服务器的思考(多线程改进)

    1. 多个客户端上传到一个服务器的思考 通过while循环可以改进一个服务器接收多个客户端. 但是这个是有问题的.如果是这种情况,假设我还有张三,李四,王五这三个人分别执行客户端  张三:好好学习.a ...

  8. 关于Eclipse中的开源框架EMF(Eclipse Modeling Framework)

    Eclipse项目本身可以划分为4个主要的子项目:Equinox,平台,Java开发工具(Java Development Tools,JDT)和插件开发环境(Plug-in Development ...

  9. C#下解决DrawImage画出来的Image变大了的问题

    如: private Image image= Resources.image1;//假设image1这张资源图是360×600这么大 private Graphics graphics; graph ...

  10. js观察者模式

    观察者模式存在观察者和被观察者 被观察者的状态发生改变,通知观察者调用观察者的update方法,观察者的update方法对被观察者的状态进行检测,做出相应的操作 被观察者存在接口attach,deta ...