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. 熟悉pyspider的装饰器

    熟悉pyspider的装饰器取经地点:https://segmentfault.com/a/1190000002477863 @config(age=10 * 24 * 60 * 60) 在这表示我们 ...

  2. opencv2\flann\matrix.h(69): error C2059: 语法错误:“,”

    在提示错误的matrix.h头文件中,修改一下,在free前加上_ ,即FLANN_DEPRECATED void _free() .

  3. MySQL dump文件导入

    1 打开cmd 输入要导入的数据库,用户名,密码,dump文件路径 mysql -u employees <E:\employees_db\load_departments.dump

  4. [转帖]Docker的数据管理(volume/bind mount/tmpfs)

    Docker(十五)-Docker的数据管理(volume/bind mount/tmpfs) https://www.cnblogs.com/zhuochong/p/10069719.html do ...

  5. MyBatis映射文件6

    之前说了由Employee找Department,这一节讲一讲由Department找Employee,显然前者是多对一的关系,而后者是一对多的关系. Department的JavaBean: pri ...

  6. Appium之开发环境搭建

    1.下载Appium 去官方网站下载http://appium.io/# : 本次以appium-desktop-setup-1.8.0.exe 文件为例,使用桌面版就不再需要下载server版本: ...

  7. C# Web开发中弹出对话框的函数[转载]

    public void Alert(string str_Message) { ClientScriptManager scriptManager =((Page)System.Web.HttpCon ...

  8. Jetson TX1 install py-faster-rcnn

    Install py-faster-rcnn following the official version  https://github.com/rbgirshick/py-faster-rcnn ...

  9. layui laydate设置当前日期往后不可选

    layui laydate设置当前日期往后不可选 laydate.render({ elem: '#demo', max: maxDate() }); // 设置最大可选的日期 function ma ...

  10. maven+Spring+SpringMVC+Hibernate快速搭建

    目录结构: pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...