hdu 1024 Max Sum Plus Plus(最大m段子序列和)
来源:互联网 发布:邱少云违背生理学知乎 编辑:程序博客网 时间:2024/06/05 21:10
Max Sum Plus Plus
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
分析:用dp[i][j]表示前j个数取出i段得到的最大值,那么状态转移方程为dp[i][j]=max(dp[i][j-1]+a[j],dp[i-1][k]+a[j]) , i-1<=k<=j-1
也就是说有两种不同的选择:第一个是第j个连在第j-1个所在的段的后面,第二个是第j个为新的一段的第一个数字。
但是如果n^3写的话,分分钟TLE。。而且二维数组也开不了那么大,所以要压缩成一维数组。
因为dp[i-1][t]的值只在计算dp[i][j]的时候用到,那么没有必要保存所有的dp[i][j] for i=1 to m,这样我们可以用一维数组存储。
用pre[j]表示前一个状态中1–j的dp的最大值,就变成 dp[j] = max(dp[j-1] + a[j] ,pre[j-1]+a[j])(具体详见代码)
代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define maxn 1000010const int inf=0x3f3f3f3f;int dp[maxn],pre[maxn],a[maxn];int n,m;int main(){ while(~scanf("%d%d",&m,&n)) { memset(dp,0,sizeof(dp)); memset(pre,0,sizeof(pre)); for(int i=1; i<=n; ++i) scanf("%d",&a[i]); int maxx; for(int i=1; i<=m; ++i) { maxx=-inf; for(int j=i; j<=n; ++j) { dp[j]=max(dp[j-1],pre[j-1])+a[j]; pre[j-1]=maxx;//为i+1做准备 if(dp[j]>maxx) maxx=dp[j];//保存前j个段数为i时的最大值 } } printf("%d\n",maxx); } return 0;}
总结:这道题n^3的能推出来,但是压缩成一维的却一点也不好写。。
一是对第二个for循环的范围没有把握好,没有想清楚j必须从i开始
二是化成一维时对最终的结果必须从dp[m][1~n]中得出这一点没有控制好
三是对pre[]更新的时间没有想清楚,每一次更新pre[]实际上是为下一个状态做准备,不能影响本次的状态
- hdu 1024 Max Sum Plus Plus(最大m段子序列和)
- hdu Max Sum Plus Plus(最大m段子段和)
- hdu Max Sum Plus Plus(最大m段子段和)
- hdu1024 Max Sum Plus Plus(M段子序列的最大和)
- [HDU 1024] Max Sum Plus Plus 多段子序列和
- HDU 1024 最大m段子段和 Max Sum Plus Plus
- HDU 1024 Max Sum Plus Plus((最大m段子段和))(动态规划经典)
- hdu 1024 Max Sum Plus Plus 最大m个子序列
- !HDU 1024 Max Sum Plus Plus-dp-(分组dp?最大分段子序列和)
- HDU 1024 Max Sum Plus Plus(求m个不相交连续子序列最大和/01背包)
- HDOJ 1024 Max Sum Plus Plus 最大K段子序列和(01背包 + 滑动数组 + 优化)
- hdu 1024 Max Sum Plus Plus(最大M子段和)
- hdu 1024 Max Sum Plus Plus(最大m子段和)
- HDU 1024 Max Sum Plus Plus(最大M子段和)
- hdu 1024 Max Sum Plus Plus(最大m字段和)
- hdu 1024 Max Sum Plus Plus(m段最大子列和)
- hdu 1024 最大M子段和 Max Sum Plus Plus(dp)(中等难度)
- HDU 1024 Max Sum Plus Plus(最大M段和)
- JavaScript常用工具函数(验证数据格式、清除空格、判断浏览器类型、cookie操作等)
- Android中判断网络连接是否可用及监控网络状态
- volatile的java弱一致性(ConcurrentHashMap 中get 是不加锁的)后补
- 每日一题 No.35 五一劳动节快乐~
- gzip
- hdu 1024 Max Sum Plus Plus(最大m段子序列和)
- 反转字符串
- Oracle 约束的增删
- UVA 1103 Ancient Messages dfs
- ArcGIS Python API获取以及对比landsat数据
- float的影响
- POJ2828 Buy Tickets(线段树,单点更新)
- jQuery来了--进行CSS操作的方法(添加/删除类),CSS()方法
- poj2635(素数表)