HDOJ 题目1024 Max Sum Plus Plus(动态规划,不想交子段最大和)
来源:互联网 发布:乐乎城市青年社区北苑 编辑:程序博客网 时间:2024/06/05 05:44
Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17234 Accepted Submission(s): 5674
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. ^_^
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.
Process to the end of file.
Output
Output the maximal summation described above in one line.
Sample Input
1 3 1 2 32 6 -1 4 -2 3 -2 3
Sample Output
68HintHuge input, scanf and dynamic programming is recommended.
Author
JGShining(极光炫影)
Recommend
We have carefully selected several similar problems for you: 1074 1025 1080 1160 1078
思路:http://blog.csdn.net/jdplus/article/details/19974647
设状态为 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]}。
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]}。
ac代码
#include<stdio.h>#include<string.h>#define INF 0xfffffff#define max(a,b) (a>b?a:b)int dp[1000100],a[1000100],pre[1000100];int main(){int n,m;while(scanf("%d%d",&m,&n)!=EOF){int i,ans,max,j;for(i=1;i<=n;i++){scanf("%d",&a[i]);}memset(dp,0,sizeof(dp));memset(pre,0,sizeof(pre));for(i=1;i<=m;i++){max=-1*INF;for(j=i;j<=n;j++){dp[j]=max(dp[j-1]+a[j],pre[j-1]+a[j]);pre[j-1]=max;;//这两条语句不能写反了,pre[j-1]表示的是上一个状态中i...j-1的最大值,max更新之后表示的i...j的最大值,if(dp[j]>max)max=dp[j];}}printf("%d\n",max);}}
0 0
- HDOJ 题目1024 Max Sum Plus Plus(动态规划,不想交子段最大和)
- hdoj 1024 Max Sum Plus Plus 【动态规划经典题目】【m子段和】
- HDOJ 1024 Max Sum Plus Plus -- 动态规划
- hdoj 1024 Max Sum Plus Plus 【动态规划】
- HDOJ 1024 Max Sum Plus Plus 最大M字段和
- HDU 1024 Max Sum Plus Plus((最大m段子段和))(动态规划经典)
- HDU 1024:Max Sum Plus Plus 经典动态规划之最大M子段和
- HDU1024 Max Sum Plus Plus 最大子段和(动态规划)
- hdoj problem 1024Max Sum Plus Plus(动态规划&&DP问题)
- HDU 1024 Max Sum Plus Plus(动态规划)
- HDU:1024 Max Sum Plus Plus(动态规划)
- HDOJ-1024 Max Sum Plus Plus (最大M子段和问题)
- hdoj 1024 Max Sum Plus Plus(m个子段最大和)
- HDOJ-1024 Max Sum Plus Plus (最大M子段和问题)
- hdoj Max Sum Plus Plus Plus 1244 (DP)m个连续段的最大和
- hdu 1024 Max Sum Plus Plus(动态规划+m子段和的最大值)
- 最大m子段和问题 Max Sum Plus Plus —— 动态规划
- hdu Max Sum Plus Plus(动态规划+分段最大子段和)
- 黑马程序员-day16Map集合及Map扩展
- NGUI 资源下载与教程专区
- 差之毫厘,谬以千里
- java 深入分析Java ClassLoader原理(实践操作)
- JSP 中集成JQuery 插件的实用方法
- HDOJ 题目1024 Max Sum Plus Plus(动态规划,不想交子段最大和)
- 迷宫深度优先搜索 找出一条可行路径
- 【Python】网络爬虫
- 开博
- Sort List
- python urllib
- SSH三大框架的工作原理及流程
- 比较好的博客
- Linux常用基本配置