博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1481:Maximum sum
阅读量:4599 次
发布时间:2019-06-09

本文共 1560 字,大约阅读时间需要 5 分钟。

题目链接

描述Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below:

t1     t2          d(A) = max{ ∑ai + ∑aj | 1 <= s1 <= t1 < s2 <= t2 <= n }                     i=s1   j=s2

Your task is to calculate d(A).输入The input consists of T(<=30) test cases. The number of test cases (T) is given in the first line of the input. 
Each test case contains two lines. The first line is an integer n(2<=n<=50000). The second line contains n integers: a1, a2, ..., an. (|ai| <= 10000).There is an empty line after each case.输出Print exactly one line for each test case. The line should contain the integer d(A).样例输入

1101 -1 2 2 3 -3 4 -4 5 -5

样例输出

13

提示In the sample, we choose {2,2,3,-3,4} and {5}, then we can get the answer.

Huge input,scanf is recommended.

这道题目的基础是最大子段和。之前学习过有关的知识,自以为可以很轻松的解决这个问题,直到亲手做了一遍才发现不是那么回事。这道题目在最大子段和的基础上强化了,要求两个不想交的子段的和的最大值。这个问题与另一道问题就很类似

核心的思路就是先求出从前到后以i为结尾的最大子段和,和到i位置的子段和的最大值。然后从后往前进行相同的过程。最后就是求解和的最大值。

代码

#include
#include
#include
#include
#include
#include
#include
#define DEBUG(x) cout<<#x<<" = "<
<
num[i]?sumf[i-1]+num[i]:num[i]; dpf[i]=max(sumf[i],dpf[i-1]); } sumr[N]=num[N]; dpr[N]=sumr[N]; for(int i=N-1;i>=1 ;i-- ){ sumr[i]=sumr[i+1]+num[i]>num[i]?sumr[i+1]+num[i]:num[i]; dpr[i]=max(sumr[i],dpr[i+1]); } int ans=-0x3f3f3f3f; for(int i=1;i<=N-1 ;i++ ){ ans=max(ans,dpf[i]+dpr[i+1]); } printf("%d\n",ans); }}
View Code

 

转载于:https://www.cnblogs.com/MalcolmMeng/p/9489301.html

你可能感兴趣的文章
List接口源码解读
查看>>
GNU Radio入门之旅
查看>>
将数据库所有表和字段首字母变成大写
查看>>
如何在vue项目中使用md5.js及base64.js
查看>>
最长公共子序列 Lcs
查看>>
关于虚拟空间上传没有权限问题 只要更改一下system.web 就可以
查看>>
C#知识点总结【1】
查看>>
BZOJ 1257: [CQOI2007]余数之和
查看>>
20155235 2016-2017-2 《Java程序设计》第六周学习总结
查看>>
H3C VLAN 配置
查看>>
BZOJ 1077: [SCOI2008]天平
查看>>
第一天
查看>>
团队冲刺第十天
查看>>
Gradle用户指南
查看>>
iOS审核策略重磅更新:Guideline 2.1批量拒审
查看>>
给 vue项目添加ESLint
查看>>
Swift3.0 功能一(持续更新)
查看>>
HexColor
查看>>
Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
查看>>
你会用swift创建复杂的加载动画吗(1)
查看>>