Max Sum Plus Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 17164    Accepted Submission(s): 5651
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.
/*
** dp[i][j]表示以第i个数字结尾且选定并分成j份能得到的最大值。转移方程为
** dp[i][j] = max(dp[i-1][j], max(dp[1...i-1][j-1])) + arr[i];
** 假设开二维数组的话内存会超,所以得用滚动数组省空间。preMax[j]保存
** 上一轮得到的dp[1...i][j]中的最大值,ans每次读取当前dp数组最大值
** 用以更新preMax数组,最后一轮循环后ans保存的就是答案。
*/ #include <stdio.h>
#include <string.h> #define maxn 1000010
#define inf 0x7fffffff int dp[maxn], preMax[maxn], arr[maxn]; int max(int a, int b) {
return a > b ? a : b;
} int main() {
int n, m, i, j, ans;
while(scanf("%d%d", &n, &m) == 2) {
for(i = 1; i <= m; ++i) {
scanf("%d", &arr[i]);
preMax[i] = dp[i] = 0;
}
preMax[0] = dp[0] = 0;
for(j = 1; j <= n; ++j) { // 分成j份
ans = -inf;
for(i = j; i <= m; ++i) {
dp[i] = max(dp[i-1], preMax[i-1]) + arr[i];
preMax[i-1] = ans;
ans = max(ans, dp[i]);
}
}
printf("%d\n", ans);
}
return 0;
}

HDU1024 Max Sum Plus Plus 【DP】的更多相关文章

  1. HDU 1024 Max Sum Plus Plus【DP】

    Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we ...

  2. hdu1024 Max Sum Plus Plus 滚动dp

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

  3. HDU 1024 Max Sum Plus Plus【DP,最大m子段和】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意: 给定序列,给定m,求m个子段的最大和. 分析: 设dp[i][j]为以第j个元素结尾的 ...

  4. HDU1024 Max Sum Plus Plus(dp)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 #include<iostream> #include<vector> #i ...

  5. HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】

    HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  6. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  7. HDOJ 1257 最少拦截系统 【DP】

    HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  8. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  9. POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】

    POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...

随机推荐

  1. dxg:TreeListView.RowDecorationTemplate

    <dxg:TreeListView.RowDecorationTemplate> <ControlTemplate TargetType="ContentControl&q ...

  2. C++ vector用法(转)

    在c++中,vector是一个十分有用的容器,下面对这个容器做一下总结. 1 基本操作 (1)头文件#include<vector>. (2)创建vector对象,vector<in ...

  3. DigCSDN介绍首页

    recrefer=SE_D_DigCSDN">360手机助手下载地址 兴许平台会陆续登陆上线的,大家敬请期待 最后由于屏幕适配和分享链接的问题,导致最后的公布时间延误了好几天--- 今 ...

  4. 缺少dll文件的解决方法

    1.什么是dll文件 从专业的角度来说,dll文件,即动态连接库,是一种不可执行的二进制文件,它允许程序共享执行特殊任务所必需的代码和其他资源.打个比方,相当于你去饭店吃饭,只人带上钱或卡就可以了,不 ...

  5. nodejs检查已安装模块

    命令行 npm ls --depth 0

  6. 【CSWS2014 Main Conference】Some Posters

    随机选了几张POSTER,之前没做过POSTER的同学可以看一下文字.图片.布局以及每个版块的小标题,以后如果需要做poster就容易多了. 据说这种Poster一张需要60RMB左右. 其中第5幅是 ...

  7. 设置tableViewCell背景颜色

    1 2 3 4 5 6 7 8 9 10 11 12 13 //方法一: cell.contentView.backgroundColor = [UIColor redColor]; //方法二: U ...

  8. 在MyEclipse中设置jsp页面为默认utf-8编码(转)

    http://www.cnblogs.com/xdp-gacl/p/3496161.html 在MyEclispe中创建Jsp页面,Jsp页面的默认编码是“ISO-8859-1”,如下图所示: 在这种 ...

  9. 选择合适的Linux版本

    以下纯属个人想法:欢迎指正 1.Linux桌面系统,首选Ubuntu.当然Ubuntu也有服务器版本,不建议使用 2.服务端的Linux稳定的服务器系统,在企业中从事运维工作Redhat或者CentO ...

  10. Gradle修改Maven仓库地址

    博客已经搬家https://www.tianmingxing.com 背景 不知从什么时候大家开始使用gradle管理项目了,随着时间的推移从maven转过来的人肯定越来越多.关于gradle的优势在 ...