Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23844    Accepted Submission(s): 8143

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
 
 
 
/**
 
设 Num 为给定数组,n 为数组中的元素总数,Status[i][j]表示前 i 个数
在选取第 i 个数的前提下分成 j 段的最大值,其中 1<=j<=i<=n && j<=m,状态转移方程为:
Status[i][j]=Max(Status[i-1][j]+Num[i],Max(Status[0][j-1]~Status[i-1][j-1])+Num[i]);
 
dp[i][j] 代表前 j 个数,组成 i 组的和的最大值
决策: 第 j 个数,是包含在第 i 组里面,还是自己独立成组
方程  dp[i][j] = max(dp[i][j-1]+a[j], max(dp[i-1][k]) + a[j]) 0<k<j
空间复杂度,m 未知,n<=1000000, 继续滚动数组
时间复杂度,n^3,  n<=1000000,显然会超时,继续优化
 
max(dp[i-1][k]) 就是上一组0...j-1的最大值。我们可以在每次计算dp[i][j]的时候记录
下前 j 个的最大值用数组保存下来,下次计算的时候可以用,这样时间复杂度为n^2
 
**/
 
 

这里的最小情况是只分成一段的时候,就退化为最大子段和问题了,这个是段数的最小情况了; 如果只有0个数的时候,结果肯定为零了,或者如果只有一个数的时候就是这个数了,那么数列只有0个或者1个的时候就是数组的最小情况了。

然后记录使用一个数组记录dp[MAX_N],其中dp[i]的含义就是在一定选择i这个数的时候得到的最大和值。

这样如果分成i段,有j个数的时候dp[j] = max(dp[j-1]+arr[j], MAX[j-1]+arr[j]),其实如果使用二维数组,那么MAX[j-1] == dp[[i-1][j-1]这个状态格的数值,表示分成i-1段的时候,有j-1个数的值。取max就是表示arr[j]是直接和dp[j-1]即接在后面第i段,还是独立成为一段的值比较大,选择最优方案,取最大值。

 
 
#include <stdio.h>
#include <vector>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <limits.h>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std; const int MAX_N = ;
int dp[MAX_N], MAX[MAX_N], arr[MAX_N]; int main()
{
int n, m, maxSum;
while (~scanf("%d %d", &m, &n))
{
memset(MAX, , sizeof(int) * (n+));
memset(dp, , sizeof(int) * (n+));
for (int i = ; i <= n; i++)
{
scanf("%d", arr+i);
}
for (int i = ; i <= m; i++)
{
maxSum = INT_MIN;
for (int j = i; j <= n; j++)
{
dp[j] = max(dp[j-]+arr[j], MAX[j-]+arr[j]);
MAX[j-] = maxSum;
maxSum = max(maxSum, dp[j]);
}
}
printf("%d\n", maxSum);
}
return ;
}

(最大m子段和) Max Sum Plus Plus (Hdu 1024)的更多相关文章

  1. Max Sum Plus Plus HDU - 1024

    Max Sum Plus Plus     HDU - 1024 Now I think you have got an AC in Ignatius.L's "Max Sum" ...

  2. 最大m段子段和 Day9 - E - Max Sum Plus Plus HDU - 1024

    Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we ...

  3. C - Max Sum Plus Plus HDU - 1024

    用二位数组dp[i][j]记录组数为i,前j个数字的最大子段和. 转移方程: dp[i][j],考虑第j个数,第j个数可以并到前面那一组,此时dp[i][j]=dp[i][j-1]+arr[j],第j ...

  4. Max Sum Plus Plus HDU - 1024 基础dp 二维变一维的过程,有点难想

    /* dp[i][j]=max(dp[i][j-1]+a[j],max(dp[i-1][k])+a[j]) (0<k<j) dp[i][j-1]+a[j]表示的是前j-1分成i组,第j个必 ...

  5. 【题解】最大 M 子段和 Max Sum Plus Plus [Hdu1024] [51nod1052]

    [题解]最大 M 子段和 Max Sum Plus Plus [Hdu1024] [51nod1052] 传送门:最大 \(M\) 子段和 \(Max\) \(Sum\) \(Plus\) \(Plu ...

  6. 最大子段和(Max Sum)

    Max Sum. The following is an instance. a)    (-2,11,-4,13,-5,-2) 思路: 最大子段和:给定一个序列(元素可正可负),找出其子序列中元素和 ...

  7. HDOJ-1003 Max Sum(最大连续子段 动态规划)

    http://acm.hdu.edu.cn/showproblem.php?pid=1003 给出一个包含n个数字的序列{a1,a2,..,ai,..,an},-1000<=ai<=100 ...

  8. [ACM] hdu 1003 Max Sum(最大子段和模型)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  9. HDU 1024 Max Sum Plus Plus【动态规划求最大M子段和详解 】

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. 5D - Rectangles

    Given two rectangles and the coordinates of two points on the diagonals of each rectangle,you have t ...

  2. hdu 2571 (命运) 那个配图女神

    http://acm.hdu.edu.cn/showproblem.php?pid=2571 枚举每一个点,找出按照题目要求的这个点的上一点的最大值,合并到当前点,注意只取前面的一种情况 #inclu ...

  3. java中 this 关键字的三种用法

    Java中this的三种用法 调用属性 (1)this可以调用本类中的任何成员变量 调用方法(可省略) (2)this调用本类中的成员方法(在main方法里面没有办法通过this调用) 调用构造方法 ...

  4. de4dot破解脱壳新版MaxtoCode源数组长度不足解决办法

    之前在看雪混了4年.NET破解版主,现在转战这里,发现很多人还在玩的是工具类的破解,可以说这里的人都还是皮毛啊 最近很多人问使用de4dot脱壳MaxtoCode有问题,之前写过一个教程,那是工具篇的 ...

  5. Spring MVC 请求处理方法

    以下两种都可以处理用户请求,但请求处理方法值得是第二种 1. SpringMVC 提供的 Controller 接口中公开的 ModelAndView handleRequest(request, r ...

  6. Java在dos界面运行java源文件编译成功,但运行虚拟机时出现错误:“找不到或无法加载主类”的问题

    (一)首先检查环境变量配置有没有问题, 1PATH为%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; 2CLASSSPATH为.;%JAVA_HOME%\lib\dt.jar; ...

  7. 【Linux】开机自动启动脚本

    Linux下(以RedHat为范本)添加开机开机自动启动脚本有两种方式; 本例系统:Linux(CentOS 7.2) 方法一 使用 /etc/rc.d/rc.local,自动启动脚本 #!/bin/ ...

  8. 树莓派无法挂载exfat格式硬盘

    ubutnu系统 挂载硬盘时报错: mount: unknown filesystem type 'exfat' 这是因为树莓派默认无法识别 exfat, 需要安装 exfat-fuse . sudo ...

  9. 还一道区间DP -- MZOJ 1346: 不老的传说

    http://10.37.2.111/problem.php?id=1346 与上一道染色基本一样,就加了个限制条件(一次最多刷maxd) #include <bits/stdc++.h> ...

  10. node 报错:Uncaught Error: Cannot find module "!!../../../node_modules/extract-webpack-plugin/loader.js

    问题出在缺少less和less-loader  因为以上模块使用了less解析. 解决方法在dependencies添加 "less": "^2.7.1", & ...