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. Linux kernel模块管理相关详解

    Linux内核模块化设计 1. Linux内核设计:单内核.模块化(动态装载和卸载) (1) Linux:单内核设计,但充分借鉴了微内核体系的设计的优点:为内核引入了模块化机制:(2) 内核的组成部分 ...

  2. 剑指offer面试题12-打印1到最大的n位数

    题目: 输入一个数字n,按顺序打印出从1最大的n位十进制数.比方输入3,则打印出1.2.3最大的三位数即999 这道题的主要陷阱就在大数的处理,仅仅要将这个考虑进去,用字符串来表示.就好说了. 那差点 ...

  3. NSDate 时区转换问题

    一: NSDateFormatter *dateFormater = [[NSDateFormatter alloc]init]; [dateFormater setDateFormat:@" ...

  4. jQuery(一)引入

    一.jQuery简介 jQuery是一个兼容多浏览器的javascript库,核心理念是write less,do more(写得更少,做得更多) 二.安装 2.1.下载 下载地址:http://jq ...

  5. 36个Android开发常用代码片段

    //36个Android开发常用代码片段 //拨打电话 public static void call(Context context, String phoneNumber) { context.s ...

  6. Redis学习(6)-常用命令

    List命令 value值为LinkedList类型. 使用环境: 1,做大数据集合的增删. 2,任务队列.用户任务队列 链表查看 lrange key start end:获取链表从start到en ...

  7. CAS 5.1.x 的搭建和使用(一)—— 通过Overlay搭建服务端

    CAS单点登录系列: CAS 5.1.x 的搭建和使用(一)—— 通过Overlay搭建服务端 CAS5.1.x 的搭建和使用(二)—— 通过Overlay搭建服务端-其它配置说明 CAS5.1.x ...

  8. yum插件yum-fastestmirror

    yum多个mirror自动选择速度最快的mirror,yum-fastestmirror插件,它会自动选择最快的mirror 配置文件: /etc/yum/pluginconf.d/fastestmi ...

  9. hdu1690Bus System--解题报告

    题意:有一个公交系统的收费标准例如以下表: 然后问:给出 这些L1~4 & C1~4的值,然后 N个站.列出每一个站的X坐标.然后询问M次,问两个站台的最小花费 题解:那么这里非常明显是最短路 ...

  10. 【laravel5.4】使用baum\node 类库实现无限极分类

    1.在model中引入baum\node 类库,并继承,具体参考 https://packagist.org/packages/baum/baum 2.核心代码: /* * model::create ...