Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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 S 1, S 2, S 3, S 4 ... S x, ... S n (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ S x ≤ 32767). We define a function sum(i, j) = S i + ... + S j (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(i 1, j 1) + sum(i 2, j 2) + sum(i 3, j 3) + ... + sum(i m, j m) maximal (i x ≤ i y ≤ j x or i x ≤ j y ≤ j x 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(i x, j x)(1 ≤ x ≤ m) instead. ^_^

Input

Each test case will begin with two integers m and n, followed by n integers S 1, S 2, S 3 ... S n.
Process to the end of file.

Output

Output the maximal summation described above in one line.

Sample Input

  1.  
  2. 1 3 1 2 3
  3. 2 6 -1 4 -2 3 -2 3

Sample Output

  1.  
  2. 6
  3. 8

Hint

  1. Huge input, scanf and dynamic programming is recommended.

本题的大致意思为给定一个数组,求其分成m个不相交子段和最大值的问题。

设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])

乍看一下这个方程挺吓人的,因为题中n的限定范围为1~1,000,000而m得限定范围没有给出,m只要稍微大一点就会爆内存。但仔细分析后就会发现Status[i][j]的求解只和Status[*][j]与Status[*][j-1]有关所以本题只需要两个一维数组即可搞定状态转移。

在进行更进一步的分析还会发现其实Max(Status[0][j-1]~Status[i-1][j-1])根本不需要单独求取。在求取now_Status(保存本次状态的数组)的过程中即可对pre_Status(保存前一次状态的数组)进行同步更新。

状态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.

  1. /*
  2. 求序列s的m子段和
  3. 设计状态: dp[i][j]为最后一个字符串以s[j]为结尾的i段最大子段和
  4. 求解目标: dp[M][N]
  5. 状态转移方程为:
  6. dp[i][j]=max{dp[i][j-1]+a[j],max{dp[i-1][t]}+a[j]} i-1=<t<j-1
  7. 时间复杂度:O(M*N)
  8. 当 M 很小时可以看作是 O(N)
  9. 空间复杂度:O(M*N)
  10. 使用滚动数组可以降至 O(N)
  11. */
  12. #include<bits/stdc++.h>
  13. using namespace std;
  14. const int MAXN=1000100;
  15. const int INF=0x3f3f3f3f;
  16. int dp[MAXN];//滚动数组,以i下标为结尾的子段在当前个子段下的最大和
  17. int s[MAXN];
  18. int mm[MAXN];//存储前i-1子段时,每个下标之前的最大子段和
  19. int main()
  20. {
  21. //freopen("data.in","r",stdin);
  22. int m,n;
  23. while(~scanf("%d%d",&m,&n)){
  24. memset(dp,0,sizeof(dp));
  25. memset(mm,0,sizeof(mm));
  26. for(int i=1;i<=n;i++){
  27. scanf("%d",&s[i]);
  28. }
  29. int mmax=-INF;
  30. for(int i=1;i<=m;i++){
  31. mmax=-INF;
  32. for(int j=i;j<=n;j++){
  33. dp[j]=max(dp[j-1],mm[j-1])+s[j];
  34. mm[j-1]=mmax;
  35. mmax=max(mmax,dp[j]);//mmax存放结果
  36. }
  37. }
  38. printf("%d\n",mmax);
  39. }
  40. }

Max Sum Plus Plus(最大m字段和,优化)的更多相关文章

  1. HDU-1003 Max Sum(动态规划,最长字段和问题)

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

  2. HDU 1024 Max Sum Plus Plus(DP的简单优化)

    Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...

  3. HDU1003 Max Sum(求最大字段和)

    事实上这连续发表的三篇是一模一样的思路,我就厚颜无耻的再发一篇吧! 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 -------------- ...

  4. HDU 1024 Max Sum Plus Plus (动态规划 最大M字段和)

    Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...

  5. hdu 1024 Max Sum Plus Plus

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

  6. Max Sum Plus Plus

    A - Max Sum Plus Plus Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  7. 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 ...

  8. HDU 1003:Max Sum(DP,连续子段和)

    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个子段的最大子段和)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/ ...

随机推荐

  1. composer命令介绍之install和update及其区别

    composer 是 php 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们. 然而,对于如何『安装他们』,新手可能并不清楚.网上的答案有的说 composer in ...

  2. 33.服务之间的调用之RPC、Restful深入理解

    33.服务之间的调用之RPC.Restful深入理解 2018年05月08日 01:52:42 郑学炜 阅读数 13577更多 分类专栏: 6.框架   版权声明:本文为博主原创文章,遵循CC 4.0 ...

  3. hdu 6399 City Development

    vjudge 读进来可能会有相同的\(n_i\),不过在相同的\(n_i\)中只有最后一个是有用的,所以其他的要缩起来,缩完后这些\(n\)的数量不会超过19个 可以发现一个城市的答案为所有城市初始权 ...

  4. java 枚举enum的使用(与在switch中的使用)

    实际开发中,很多人可能很少用枚举类型.更多的可能使用常量的方式代替.但枚举比起常量来说,含义更清晰,更容易理解,结构上也更加紧密.看其他人的博文都很详细,长篇大论的,这里理论的东西不说了,一起看看在实 ...

  5. 转载Google TPU论文

    选自 Google Drive 作者:Norman P. Jouppi 等 痴笑@矽说 编译 该论文将正式发表于 ISCA 2017 从去年七月起,Google就号称了其面向深度学习的专用集成电路(A ...

  6. docker删除虚悬镜像(临时镜像文件)

    在我们构建镜像的过程中,常常需要使用build命令根据Dockerfile进行构建镜像,并且会build很多次,镜像名字也是相同的,那么就会出来下面这种情况

  7. 数据库命令行工具USQL、mycli、litecli、pgcli

    USQL USQL 是一款使用 Go 语言开发的支持 SQL/NoSQL 数据库的通用命令行工具,它支持多种主流的数据库软件,目前最新版本是usql 0.7.0.比如 PostgreSQL.MySQL ...

  8. Swift(三)基本运算符

    Swift支持大部分标准C语言的运算符,并且对许多特性进行改进来减少常规编码的错误.除了支持基本运算符外,Swift还提供了2个特殊的运算符,分别是:溢出运算符和区间运算符 首先看下基本运算符 imp ...

  9. iOS获取APP的版本号和名称

    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; CFShow(infoDictionary); // ap ...

  10. IDEA打开光标是粗黑色,backspace键、insert键点击无效的解决办法

    问题描述:打开IDEA的编译器之后,界面显示的光标变粗,点击backspace键和insert键盘之后无效 解决方法:打开File——Settings——Plugins,在右侧的搜索栏中搜索IdeaV ...