Max Sum Plus Plus     HDU - 1024

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 ≤ iy ≤ 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

Output

Output the maximal summation described above in one
line.

and n, followed by n integers S 1,
S2, S 3 ... S n. 
Process to the end of file.

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.

分析:d[j]为遍历到当前数字时,前j-1个数的i段区间和的最大值加上a[j],d[j]是动态的,可取可不取;

p[j]为记录前j个数的i段区间和的最大值;

j

1

2

3

4

5

6

i

-1

4

-2

3

-2

3

1

d

-1

4

2

5

3

6

1

p

-1

4

4

5

5

2

d

-1

3

2

7

5

8

2

p

-1e9

3

2

7

7

代码中p[n]没有记录,直接放在了ans中。

代码:

#include<stdio.h>

#include<string.h>

#include<iostream>

#include<math.h>

using
namespace std;

const int
Max=1e6+5;

int
p[Max],a[Max],d[Max];

int main()

{

int m,n,ans;

while(scanf("%d%d",&m,&n)!=EOF)

{

memset(p,0,sizeof(p));

memset(d,0,sizeof(d));

for(int i=1;i<=n;i++)

scanf("%d",&a[i]);

for(int i=1;i<=m;i++)

{

ans=-1e9;

for(int j=i;j<=n;j++)

{

d[j]=max(d[j-1],p[j-1])+a[j];

p[j-1]=ans;

ans=max(ans,d[j]);

}

}

printf("%d\n",ans);

}

}

Max Sum Plus Plus HDU - 1024的更多相关文章

  1. 最大m段子段和 Day9 - E - Max Sum Plus Plus HDU - 1024

    Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we ...

  2. Max Sum Plus Plus HDU - 1024 基础dp 二维变一维的过程,有点难想

    /* dp[i][j]=max(dp[i][j-1]+a[j],max(dp[i-1][k])+a[j]) (0<k<j) dp[i][j-1]+a[j]表示的是前j-1分成i组,第j个必 ...

  3. C - Max Sum Plus Plus HDU - 1024

    用二位数组dp[i][j]记录组数为i,前j个数字的最大子段和. 转移方程: dp[i][j],考虑第j个数,第j个数可以并到前面那一组,此时dp[i][j]=dp[i][j-1]+arr[j],第j ...

  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(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Problem Description Now I think you ...

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

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

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

  9. (最大m子段和) Max Sum Plus Plus (Hdu 1024)

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

随机推荐

  1. Android adb 串口调试

    adb (串口输入) echo 1 > /sys/class/remount/need_remount; mount -o rw,remount /system                  ...

  2. javascript的数组之map()

    map()方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的回调函数后返回的结果.新数组 // ES6 let numbers = [1, 5, 10, 15]; let doubles ...

  3. PHP字符串处理 单引号 双引号 heredoc nowdoc 定界符

    ---恢复内容开始--- 2019年2月22日09:49:54 参考文档:   http://php.net/manual/zh/language.types.string.php#language. ...

  4. 《SQL Server性能调优实战》知识点汇总

    2.4数据库结构的设计 好的性能出自好的设计 尽可能添加数据完整约束(非空约束.默认值约束.CHECK约束.唯一约束.外键约束)等,这些约束的添加将有助于数据库关系引擎分析执行计划. 尽可能小的字段类 ...

  5. 判断为false的情况

    console.log(''==false)  //true console.log('0' == false)  //true console.log(null == false) //false, ...

  6. Delphi 的内存操作函数(2): 给数组指针分配内存

    静态数组, 在声明时就分配好内存了, 譬如: var   arr1: ..] of Char;   arr2: ..] of Integer; begin   ShowMessageFmt('数组大小 ...

  7. C语言学习随笔记

    第一次接触C语言,心中对新知识还是充满好奇的.最开始是从晓鹏老师那听说的C语言,记得当时晓鹏老师是在给我们介绍软考,叫我们去准备软考的时候说到了C语言告诉我们C语言是基础,C语言很重要,叫我们能学多好 ...

  8. jmeter 之 if controller

    jmeter版本5.0.下面是jmeter5.0的if逻辑控制器的截图 标红字体的意思大概是,如果勾选了 下面的 interpret condition as variable expression ...

  9. Spring Boot(二):Spring-Data-JPA操作数据库( Hibernate)增删改查

    一.Maven使用3.3.9版本或以上,选择Binary 版本 二.添加spring-data-jpa和数据库依赖,以oracle为例 三.添加连接数据库配置 四.新建model自动生成数据库表(不用 ...

  10. 浅谈最长上升子序列(LIS)

    一.瞎扯的内容 给一个长度为n的序列,求它的最长上升子序列(LIS) 简单的dp n=read(); ;i<=n;i++) a[i]=read(); ;i<=n;i++) ;j<i; ...