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. SyntaxError: invalid character in identifier(Python)

    在写博客时直接将博客上的代码复制运行后发现错误SyntaxError: invalid character in identifier,我以为是l(小L)写成了1,改了还是不行. 上网查了下,发现原来 ...

  2. 使用DevExpress.XtraTabbedMdi.XtraTabbedMdiManager控件来加载MDI窗体

    使用DevExpress.XtraTabbedMdi.XtraTabbedMdiManager控件来加载MDI窗体     [csharp] view plaincopyprint? <SPAN ...

  3. mongodb 参数优化

    1.大部分IO操作为随机IO,建议采用SSD或PCIE,普通硬盘RAID10 2.IO调度算法.普通磁盘:deadline避免IO请求出现“饥饿”现象,SSD/PCIE:noop简单的先进先出处理请求 ...

  4. struct与class的区别

    C++中的struct是对C中struct的扩充,它已经不再只是一个包含不同数据类型的数据结构,因为其扩充了太多功能.总的来说,C++中struct和class极其相似,比如,struct能包含成员函 ...

  5. IDEA导入Git项目后右键项目找不到Git选项的解决方法

    进入Setting -> version Control -> 在下图的第二步中是否有灰色的Module,选中它->点击第三步的+即可 转自:https://blog.csdn.ne ...

  6. MYSQL 比较集

    1.什么是较对集合:字符集的字符比较规则(collation,collate),一个字符集有多个较对集合. mysql> create table ss (id int primary key ...

  7. python练习题-day19

    1.将字符串的时间"2017-10-10 23:40:00"转换为时间戳和时间元组 import time s="2017-10-10 23:40:00" st ...

  8. jq点击事件不生效,效果只闪现一次又立马消失的原因?

    出现的问题:jq点击事件不生效,点击的时候效果实现但又立马消失,页面重新刷新了一次 可能出现的原因: a标签href属性的原因,虽然点击事件生效,但页面又刷新了一次,所以没有效果,只闪了一次 解决方案 ...

  9. 与图论的邂逅03:Lengauer-Tarjan

    回想一下,当我们在肝无向图连通性时,我们会遇到一个神奇的点——它叫割点.假设现在有一个无向图,它有一个割点,也就是说把割点删了之后图会分成两个联通块A,B.设点u∈A,v∈B,在原图中他们能够互相到达 ...

  10. Leetcode: Sliding Window Median

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...