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. 黑盒测试实践——day03

    一.任务进展情况 目前基本确定选取的测试工具是Testwriter,测试的web系统还在待定状态,小组成员都在网上搜集相关知识,学习相关的测试技术.        二.存在的问题 Testwriter ...

  2. cpu读取指令时读取的长度

    CPU读取指令时,如果单字节指令,一次访存即可完成读取操作:如果是多字节指令,会根据第一次读取指令的操作码与寻址标志位,判断指令的后续长度,进而完成整个指令的读取,同时指令指针IP会自动进行修改,指向 ...

  3. php发送邮箱重置密码链接,并在重置成功后使链接失效 (ThinkPHP5)

    /** * 重置密码页,验证链接有效性,页面发送邮件调用sendResetPwdEmail()方法 */ public function resetPwd() { $param = input('') ...

  4. WithOne 实体关系引起 EF Core 自动删除数据

    最近遇到了一个 EF Core 的恐怖问题,在添加数据时竟然会自动删除数据库中已存在的数据,经过追查发现是一个多余的实体关系配置引起的. modelBuilder.Entity<Question ...

  5. 自学stm32的一些个人经验

    1.首先我们先看看与STM32相关的文档 我们假定大家已经对STM32的书籍或者文档有一定的理解.如不理解,请立即阅读STM32的文档,以获取最基本的知识点. 如果你手上拥有ST官方主推的STM32神 ...

  6. Java多线中基础知识整理

    Java多线程中相关术语: 一.Java多线程原则 1.原子性:一个操作或者多个操作要么全部执行并且执行的过程不会被任何因素打断,要么就都不执行.一般使用线程同步或者Lock锁来确保. 2.可见性(J ...

  7. 64IE无法显示网页

    1.碰到如下图情况ie浏览器打开之后显示不了网页,但是试了下电脑上其他的浏览器都可以正常的打开网页. 2.解决办法为:打开浏览器-选择“工具”-“Internet选项”-“高级”-重置,重启浏览器后能 ...

  8. 共享文件 Ubuntu下安装Samba与Windows

    Author:Xianghai Ding.Date:2019/01/08**************************************************************安装 ...

  9. ajax php 验证注册用户名是否存在

    1.在"test"数据库中,建立一张名为"user"的表. sql语句: create table `user`( `id` ) not null auto_i ...

  10. npm run dev/build/serve

    1.ERR引发的思考 npm run dev npm ERR! missing script: dev npm ERR! A complete log of this run can be found ...