Max Sum Plus Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 17336    Accepted Submission(s): 5701

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
Hint
Huge input, scanf and dynamic programming is recommended.

给定一个数组,求M段连续的子段和最大。dp[i][j]表示前i段选择第j个元素的最优解。

dp[i][j]=max(dp[i][j-1]+a[j] , max( dp[i-1][k] ) + a[j] ) 0<k<j

由于k<j,所以我们能够用两个一维数组,一个dp[i]记录当前行状态。一个d[i]记录下一行可选的最大值。

用64位会超时。但int能水过。。

</pre><pre name="code" class="cpp">/*
dp[i][j]=max(dp[i][j-1]+a[j] , max( dp[i-1][k] ) + a[j] ) 0<k<j
第j个元素选i段区间,对于当前元素a[j],能够把它接到第i段的第j-1位置构成一段。
或者单独成一段。 观察这个方程。对于dp[i][j-1]这一项,它和dp[i][j]同i,就是在一段。
用一维数组就能记录;
dp[i-1][k]是i-1段j位置前能取的最大值。我们能够在计算当前第i段时,
把dp[i-1][k]记录下来,取最大值就好了。
*/ #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define ll __int64
const int inf=0x7fffffff;
#define N 1000010
int a[N];
int pre[N]; //即dp[i-1][k]。记录j位置前可选值
int dp[N];
int main()
{
int i,j,n,m;
int tmp,ans;
while(~scanf("%d%d",&m,&n))
{
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
dp[i]=0;
pre[i]=0;
}
pre[0]=dp[0]=0;
ans=-inf;
for(i=1;i<=m;i++)
{
tmp=-inf;
for(j=i;j<=n;j++)
{
dp[j]=max(dp[j-1]+a[j],pre[j-1]+a[j]);
pre[j-1]=tmp; //tmp记录的是当前段j位置的最大值,
tmp=max(tmp,dp[j]);//而数组pre[]是记录j-1位置的,所以要先取tmp,再更新tmp
}
}
for(i=m;i<=n;i++)
ans=max(ans,dp[i]);
printf("%d\n",ans);
}
return 0;
}

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

  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 Plus【动态规划求最大M子段和详解 】

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

  5. HDU 1024 Max Sum Plus Plus [动态规划+m子段和的最大值]

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

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

  7. HDU 1024 max sum plus

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

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

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

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

随机推荐

  1. 02使用常规步骤编译NanoPiM1Plus的Android4.4.2

    02使用常规步骤编译NanoPiM1Plus的Android4.4.2 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 21504965 AT qq.com 完成时间:2017/12/5 17:51 版本 ...

  2. php redis 操作大全

    类和方法 用法 Redis类 类RedisException 预定义的常量 Redis类 说明:创建一个Redis客户端 例 $redis = new Redis(); 类RedisException ...

  3. url-safe base64 && base64

    1: 为什么需要base64? ASCII码一共规定了128个字符的编码,这128个符号,范围在[0,127]之间. 其中,[0,31],及127, 33个属于不可打印的控制字符. 在电子邮件传输信息 ...

  4. Caffe RPN:把RPN网络layer添加到caffe基础结构中

    在测试MIT Scene Parsing Benchmark (SceneParse150)使用FCN网络时候,遇到Caffe错误. 遇到错误:不可识别的网络层crop 网络层 CreatorRegi ...

  5. Codeforces_764_C. Timofey and a tree_(并查集)(dfs)

    C. Timofey and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  6. 用 webpack 实现持久化缓存

    什么是持久化缓存? 原文链接https://sebastianblade.com/using-webpack-to-achieve-long-term-cache/ 缓存(cache)一直是前端性能优 ...

  7. CAD如何直接打印,不出现打印对话框?

    主要用到函数说明: MxDrawXCustomFunction::Mx_Print 直接打印,不出现打印对话框,详细说明如下: 参数 说明 double ptLBx 打印的范围左下角x double ...

  8. vue过渡 & 动画---进入/离开 & 列表过渡

    (1)概述 Vue 在插入.更新或者移除 DOM 时,提供多种不同方式的应用过渡效果.包括以下工具: 在 CSS 过渡和动画中自动应用 class 可以配合使用第三方 CSS 动画库,如 Animat ...

  9. dorado 7 使用总结

    最近项目上需要,使用了dorado 7 ,总体感觉还可以,快速开发很方便,然而在方便的同时,难免有些太过繁琐,很多东西都封装了起来,会造成很多不便.因此快速开发的项目可以使用,其它的不推荐.现在打算将 ...

  10. 第3章 从Flux到Redux

    第3章 从Flux到Redux 3.1 Flux 单向数据流,React是用来替换Jquery的,Flux是以替换Backbone.js.Ember.js等MVC框架为主的. actionTypes. ...