Maximum sum
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 39599   Accepted: 12370

Description

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

Your task is to calculate d(A).

Input

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.

Output

Print exactly one line for each test case. The line should contain the integer d(A).

Sample Input

1

10
1 -1 2 2 3 -3 4 -4 5 -5

Sample Output

13

Hint

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

Huge input,scanf is recommended.

Source

POJ Contest,Author:Mathematica@ZSU

题意:求两段和最大

一开始自己想
d[i][0]前i个以i结尾选了一段
d[i][1]前i个以i结尾选了两段
然后扫描维护一个d[i][0]的最大值mx,转移

d[i][0]=max(0,d[i-1][0])+a[i];

d[i][1]=max(d[i-1][1],mx)+a[i];

初始化注意一下就行了

还有一种做法:

双向求最大字段和,最后枚举第一段的结束位置求

//两个dp函数,两种方法
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=5e4+,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int T,n,a[N];
int d[N][],ans;
void dp(){
ans=-INF;int mx=a[];
d[][]=a[];d[][]=-INF;
for(int i=;i<=n;i++){
d[i][]=max(,d[i-][])+a[i];
d[i][]=max(d[i-][],mx)+a[i];
mx=max(mx,d[i][]);
ans=max(ans,d[i][]);
}
}
void dp2(){
ans=-INF;
for(int i=;i<=n;i++) d[i][]=max(,d[i-][])+a[i];
d[n+][]=;
for(int i=n;i>=;i--) d[i][]=max(,d[i+][])+a[i];
int mx=d[][];
for(int i=;i<=n;i++){
ans=max(ans,mx+d[i][]);
mx=max(mx,d[i][]);
}
}
int main(int argc, const char * argv[]) {
T=read();
while(T--){
n=read();
for(int i=;i<=n;i++) a[i]=read();
dp();
printf("%d\n",ans);
} return ;
}

POJ2479 Maximum sum[DP|最大子段和]的更多相关文章

  1. POJ2479 Maximum sum(dp)

    题目链接. 分析: 用 d1[i] 表示左向右从0到i的最大连续和,d2[i] 表示从右向左, 即从n-1到i 的最大连续和. ans = max(ans, d1[i]+d2[i+1]), i=0,1 ...

  2. poj2479 Maximum sum

    http://poj.org/problem?id=2479 题目大意:给定一组n个整数:a ={a1, a2,…,我们定义一个函数d(a)如下: 你的任务是计算d(A).输入由T(<=30)测 ...

  3. POJ-2479 Maximum sum(动态规划)

    最大子序列和的加强版. 借助最大子序列和,分别正向和反向遍历一遍得到left和right数组(具体含义见代码注释) 然后再对left和right数组进行修正,保存从对应元素起向左或向右的最大连续和. ...

  4. hdu 5586 Sum【dp最大子段和】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5586 Sum Time Limit: 2000/1000 MS (Java/Others)    Me ...

  5. POJ 2479 Maximum sum(双向DP)

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36100   Accepted: 11213 Des ...

  6. URAL 1146 Maximum Sum(最大子矩阵的和 DP)

    Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...

  7. (线性dp 最大连续和)POJ 2479 Maximum sum

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 44459   Accepted: 13794 Des ...

  8. ural 1146. Maximum Sum

    1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...

  9. 最大子矩阵和 URAL 1146 Maximum Sum

    题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...

随机推荐

  1. 初识Mybatis框架,实现增删改查等操作(动态拼接和动态修改)

    此第一次接触Mybatis框架确实是有点不适应,特别是刚从Hibernate框架转转型过来,那么为什么要使用Mybatis框架,Mybatis框架和Hibernate框架又有什么异同呢? 这个问题在我 ...

  2. 【转】ZigBee是如何组网的?

    组网方案设计:组建一个完整的zigbee网状网络包括两个步骤:网络初始化.节点(路由器或终端)加入网络,其中节点加入网络又包括两个步骤:通过与协调器连接入网和通过已有父节点入网. 一.网络初始化:  ...

  3. JS与Jquery区别

    很多人对JS和JQuery很容易搞混淆,今天我们就相比学习下: 加载区别: var myfunction(){}; JS:1.window.onload=function(){} 2.<body ...

  4. big-endian和little-endian

    1.故事的起源 "endian"这个词出自<格列佛游记>.小人国的内战就源于吃鸡蛋时是究竟从大头(Big-Endian)敲开还是从小头(Little-Endian)敲开 ...

  5. Jsp静态包含和动态包含的区别

    1 <%@include file="xxx.jsp"%>为jsp中的编译指令,其文件的包含是发生在jsp向servlet转换的时期,而<jsp:include ...

  6. 工厂方法(Factory Method),思考

    最近看见一个关于如何通过工厂方法来重构代码的提问,发现这方面,自己还没有想得特别明白,所以,稍作总结. 只要有构造的地方,就有是用工厂方法的可能. 如果考虑到单元测试和实现的扩展,就有是用工厂方法的必 ...

  7. HTML5 学习笔记(四)——canvas绘图、WebGL、SVG

    一.Canvas canvas是HTML5中新增一个HTML5标签与操作canvas的javascript API,它可以实现在网页中完成动态的2D与3D图像技术.<canvas> 标记和 ...

  8. 码农干货系列【20】--add gtTime to Promise.js

    使用场景 在一些时候,希望一件task不能太快完成,需要大于多少时间才可以执行,就可以使用Promise的gtTime方法. 使用方式 Promise.gtTime(f1(), 5000).then( ...

  9. 从FineReport看开放式引擎API

    对于一款软件或产品,尤其是一些企业级应用的IT软件,是不可能满足所有需求的.尤其是针对业务化的产品需求,某些个性化的需求就要进行二次开发.二次开发需要API接口,无论是什么样的开发,开发人员都需要对开 ...

  10. Material Design 概念,环境和基本属性

    Material Design 概念,环境和基本属性 Material Design是随Android 5.0推出的一种设计概念, 涉及到了跨平台和设备的视觉,动态,交互设计等方面.   设计概念 M ...