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. VS2010调试技巧

    最近合作开发,代码已经完成了,但是一调试,错误一大堆,由于是合作开发,不确定是哪层的错误,得一步步得走,很是费时费力,平时调试的技巧用的不多,现在集中调试,结果有些手忙脚乱,效率也很低,所以在网上找了 ...

  2. js移除Array中指定元素

    首先需要找到元素的下标: var array = [2, 5, 9]; var index = array.indexOf(5); 使用splice函数进行移除: if (index > -1) ...

  3. 【SDN】SDN相关资料--了解一下电信领域的SDN

    SDN相关资料 数据中心架构下ospf bgp如何选择及优缺点? - 数据中心 - 知乎 组播扩展OSPF_百度百科 carrier.huawei.com/cn/products/fixed-netw ...

  4. Cognos第三方权限认证Oracle用户库

    一:概要描述 1.1:项目背景 Cognos具有强大的报表功能,但是却没有提供一个完善的用户管理体系,针对商业智能系统对数据的安全性要求,我们必须实现不同用户对不同数据的访问,确保企业级以及部门级的数 ...

  5. VS2008+Windows DDK 7的环境配置

    Mark offers some third party utilities. That's good, but I will show a more handy way (IMHO): how to ...

  6. [Node.js] Load balancing a Http server

    Let's see how to do load balancing in Node.js. Before we start with the solution, you can do a test ...

  7. [Algorithm] Count Negative Integers in Row/Column-Wise Sorted Matrix

    // Code goes here function countNegative (M, n, m) { count = ; i = ; j = m - ; && i < n) ...

  8. [AngularJS] Angular 1.3 ngMessages with ngAnimate

    Note: Can use $dirty to check whether user has intracted with the form: https://docs.angularjs.org/a ...

  9. Opera Unite如何架设自己的网站

    1使用Opera的File Sharing可以共享你的文件 2使用Opera的Stream media可以共享你的视频,有些格式甚至可以直接在浏览器端打开(比如MOV,但是需要安装Quicktime的 ...

  10. codeforces Epic Game 题解

    Simon and Antisimon play a game. Initially each player receives one fixed positive integer that does ...