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组的和的最大值。
决策: 第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. #include <cstdio>
  2. #include <map>
  3. #include <iostream>
  4. #include<cstring>
  5. #include<bits/stdc++.h>
  6. #define ll long long int
  7. #define M 6
  8. using namespace std;
  9. inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
  10. inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
  11. int moth[]={,,,,,,,,,,,,};
  12. int dir[][]={, ,, ,-, ,,-};
  13. int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
  14. const int inf=0x3f3f3f3f;
  15. const ll mod=1e9+;
  16. int m,n;
  17. int a[];
  18. int dp1[];
  19. int dp2[];
  20. int main(){
  21. //ios::sync_with_stdio(false);
  22. while(~scanf("%d%d",&m,&n)){
  23. memset(dp1,,sizeof(dp1));
  24. memset(dp2,,sizeof(dp2));
  25. for(int i=;i<=n;i++)
  26. scanf("%d",&a[i]);
  27. int maxn;
  28. for(int i=;i<=m;i++){ //枚举子序列
  29. maxn=-inf;
  30. for(int j=i;j<=n;j++){ //j = i是因为每个子序列最少1个元素
  31. dp1[j]=max(dp2[j-]+a[j],dp1[j-]+a[j]);
  32. dp2[j-]=maxn;
  33. maxn=max(maxn,dp1[j]);
  34. }
  35. }
  36. cout<<maxn<<endl;
  37. }
  38. }

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

  1. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  2. HDU 1024 Max Sum Plus Plus (动态规划)

    HDU 1024 Max Sum Plus Plus (动态规划) Description Now I think you have got an AC in Ignatius.L's "M ...

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

  4. HDU 1024 max sum plus

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

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

  6. hdu 1024 Max Sum Plus Plus DP

    Max Sum Plus Plus Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php ...

  7. hdu 1024 Max Sum Plus Plus

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

  8. hdu 1024 Max Sum Plus Plus (子段和最大问题)

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

  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. 4 Past progressive VS simple past

    1 一般过去时用来谈论过去开始和结束的活动.过去进行时用来谈论过去正在进行或者发生的活动. Why were you at office so later yesterday? I was worki ...

  2. Docker 给 故障停掉的 container 增加 restart 参数

    操作过程见图: 执行的命令比较简单: docker container update --restart=always containername 即可.

  3. Dom4j解析

    dom4j-1.6.1.jar, 这个包提供了xml解析相关的方法. 这里做一个记录,微信公众号里需要对HttpServletRequest做解析,实际上也可以用dom4j提供的方法进行解析转换. 这 ...

  4. 10分钟让你的代码更加pythonic

    参考: https://blog.csdn.net/g8433373/article/details/80709116

  5. python爬虫之scrapy文件下载

    我们在写普通脚本的时候,从一个网站拿到一个文件的下载url,然后下载,直接将数据写入文件或者保存下来,但是这个需要我们自己一点一点的写出来,而且反复利用率并不高,为了不重复造轮子,scrapy提供很流 ...

  6. Spring框架IOC和AOP的实现原理

    IoC(Inversion of Control) (1). IoC(Inversion of Control)是指容器控制程序对象之间的关系,而不是传统实现中,由程序代码直接操控.控制权由应用代码中 ...

  7. 压测工具使用(vegeta)

    一.压测工具vegeta 1.介绍 Vegeta 是一个用 Go 语言编写的多功能的 HTTP 负载测试工具,它提供了命令行工具和一个开发库. 官方地址:https://github.com/tsen ...

  8. github & markdown & collapse & table

    github & markdown collapse & table https://github.com/Microsoft/TypeScript/issues/30034 GitH ...

  9. 使用PHP对二维索引数组进行排序

    本例中 data 数组中的每个单元表示一个表中的一行.这是典型的数据库记录的数据集合. 例子中的数据如下: volume | edition -------+-------- 67 | 2 86 | ...

  10. JSTL 之 <c:out>

    jstl的<c:out value="${hello}"></c:out> EL表达式的${hello },两者一般没什么不同,但是EL表达式输出的时候回尝 ...