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.
#include <cstdio>
#include <map>
#include <iostream>
#include<cstring>
#include<bits/stdc++.h>
#define ll long long int
#define M 6
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int m,n;
int a[];
int dp1[];
int dp2[];
int main(){
//ios::sync_with_stdio(false);
while(~scanf("%d%d",&m,&n)){
memset(dp1,,sizeof(dp1));
memset(dp2,,sizeof(dp2));
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
int maxn;
for(int i=;i<=m;i++){ //枚举子序列
maxn=-inf;
for(int j=i;j<=n;j++){ //j = i是因为每个子序列最少1个元素
dp1[j]=max(dp2[j-]+a[j],dp1[j-]+a[j]);
dp2[j-]=maxn;
maxn=max(maxn,dp1[j]);
}
}
cout<<maxn<<endl;
}
}

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. Springboot自定义过滤器Filter

    前言:自己写了个Springboot项目,最近写的功能越来越多,结合业务已经要写过滤器Filter来过滤处理一些请求. 在网上看了几篇博客,总结如下: 过滤器配置方式有两种: 1.通过@WebFilt ...

  2. 创建虚拟目录失败,必须为服务器名称指定“localhost”

    关于微信开发过程,远程调试后,再次打开vs出现项目加载失败的解决办法! 第一步: 第二步:打开编辑的页面,把下图这部分直接注释掉 ok了,再加载一次,就好了!

  3. 金蝶CLOUD与EAS的区别

    1.金蝶K/3 WISE主要面向单体制造企业(主要是离散制造企业):2.金蝶K/3 Cloud主要面向业务类型单一(即主营业务单一)的.注重供应链与生产业务协同的.中小型(二层集团??)集团性企业(主 ...

  4. hadoop分布式系统架构详解

    hadoop 简单来说就是用 java写的分布式 ,处理大数据的框架,主要思想是 “分组合并” 思想. 分组:比如 有一个大型数据,那么他就会将这个数据按照算法分成多份,每份存储在 从属主机上,并且在 ...

  5. Hbase数据表解析

    demo为表的命名空间,user为表的名字you2个列族,一个为b.一个为o. NAME 为列族名,Replication_SCOPE实现一个远程集群的复制.compression数据压缩的类型 Hb ...

  6. Golang的channel使用以及并发同步技巧

    在学习<The Go Programming Language>第八章并发单元的时候还是遭遇了不少问题,和值得总结思考和记录的地方. 做一个类似于unix du命令的工具.但是阉割了一些功 ...

  7. linux 挂载windows下目录,其它linux机器nfs的目录,自己dd的文件

    如有转载,不胜荣幸.http://www.cnblogs.com/aaron-agu/ 挂载window下共享的目录 //192.168.0.11/share /mnt 挂载其它linux机器下目录 ...

  8. 在文件保存中 os.getcwd() os.listdir() os.makedirs() os.mkdir() xx.join() ... 等函数 的使用介绍

    path = 'C:\\Users\\zhangjunming\\Desktop\\PycharmProjects\\my_mgm' 1.xx.join(obj)   以xx为分隔符 对obj中的元素 ...

  9. APP需求调研、对比

    二.人脸验证 1.芝麻认证 : 0.4元/次,需要企业企业认证.不能有与芝麻信用类似的业务,如:保险... 2.旷视 : 0.5/次.企业认证.业务限制 3. 百度人脸识别 :  企业认证. 4.科大 ...

  10. 为何CPU散片这么便宜?盒装CPU值得买吗

    当玩家选择装一台PC电脑的时候,他会有个怎样的思考过程?第一个要决定的通常是选什么样的处理器,因为处理器的选择可以决定整套平台的预算及性能水平,想玩游戏的话现在4核8线程处理器是入门标准了,高点的则会 ...