Maximum sum

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 39089   Accepted: 12221

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.

分别求出两端开始的最大子段和,然后枚举左右两段的分界,找出最大值。

 //2016.8.21
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; const int N = ;
const int inf = 0x3f3f3f3f;
int a[N], dpl[N], dpr[N];//dpl[i]表示从左往右到第i位的最大子段和,dpr[i]表示从右往左到第i位的最大子段和 int main()
{
int T, n;
cin>>T;
while(T--)
{
scanf("%d", &n);
for(int i = ; i < n; i++)
{
scanf("%d", &a[i]);
}
memset(dpl, , sizeof(dpl));
memset(dpr, , sizeof(dpr));
//从左往右扫
//*************************************************
dpl[] = a[];
for(int i = ; i < n; i++)
if(dpl[i-]>) dpl[i] = dpl[i-]+a[i];
else dpl[i] = a[i];
for(int i = ; i < n; i++)
if(dpl[i]<dpl[i-])
dpl[i] = dpl[i-];
//从右往左扫
//*************************************************
dpr[n-] = a[n-];
for(int i = n-; i>=; i--)
if(dpr[i+]>) dpr[i] = dpr[i+]+a[i];
else dpr[i] = a[i];
for(int i = n-; i>=; i--)
if(dpr[i]<dpr[i+])
dpr[i] = dpr[i+];
//*************************************************
int ans = -inf;
for(int i = ; i < n-; i++)
{
ans = max(ans, dpl[i]+dpr[i+]);
}
cout<<ans<<endl;
} return ;
}

POJ2479(dp)的更多相关文章

  1. POJ2479【DP 枚举】

    题意:给出一串数字,求出其中不重不交的两个子串的和的最大值 思路:最近最大子串和做多了,感觉这题有点水.枚举分割点,将序列分成左右两串,然后看左右串的最大子串和的最大值. //poj2479 #inc ...

  2. POJ2479 Maximum sum[DP|最大子段和]

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

  3. poj2479(dp)

    题目链接:http://poj.org/problem?id=2479 题意:求所给数列中元素值和最大的两段子数列之和. 分析:从左往右扫一遍,b[i]表示前i个数的最大子数列之和. 从右往左扫一遍, ...

  4. 最长子序列dp poj2479 题解

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 44476   Accepted: 13796 Des ...

  5. POJ2479 Maximum sum(dp)

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

  6. POJ2479,2593: 两段maximum-subarray问题

    虽然是两个水题,但是一次AC的感觉真心不错 这个问题算是maximum-subarray问题的升级版,不过主要算法思想不变: 1. maximum-subarray问题 maximum-subarra ...

  7. 跟着大佬重新入门DP

    数列两段的最大字段和 POJ2479 Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41231 Acce ...

  8. DP的学习

    DP在ACM的算法里面可算是重中之重,题目类型千变万化,题目难度差异也很大.是一种很讲究技巧的算法,而且代码实现相对容易,1y率非常高(除有些bt数据外).总之DP就是一向非常重要,又非常博大精深的算 ...

  9. DP入门练习

    T1 题目:codevs4815江哥的dp题a codevs4815 一个简单的DP,注意开long long(不然会全WA),以及初始条件(这题有负数,所以要把f设成极小值.还要保证转移正确). # ...

随机推荐

  1. CodeForces 662D International Olympiad

    写出前几个找规律,然后直接输出. #include<cstdio> #include<cstring> #include<cmath> #include<al ...

  2. php 中的魔术方法-----“事件方法”

    来源:http://lornajane.net/posts/2012/phps-magic-__invoke-method-and-the-callable-typehint php 中的这个对象 , ...

  3. STL中的所有算法(70个)

    STL中的所有算法(70个)----9种类型(略有修改by crazyhacking) 参考自: http://www.cppblog.com/mzty/archive/2007/03/14/1981 ...

  4. mvc中怎么读取htm的文件

    @Html.Raw(File.ReadAllText(Server.MapPath("/Include/head01.htm")))

  5. (简单) HDU 3397 Sequence operation,线段树+区间合并。

    Problem Description lxhgww got a sequence contains n characters which are all '0's or '1's. We have ...

  6. STM32的USART DMA传输(转)

    源:STM32的USART DMA传输 问题描述: 我有一个需求,AD采得一定数目的数据之后,由串口DMA发出,由于AD使用双缓冲,所以每次开始DMA的时候都需要重新设置开始的内存地址以及传输的数目( ...

  7. javascript基础教程学习总结(1)

    摘自javascript基础教程 开始: 1.将脚本放在哪里: 1.1 放在html和<html>之间 范例: <!DOCTYPE html PUBLIC "-//W3C/ ...

  8. webstorm快捷键说明

    webstorm快捷键说明 Editing编辑相关快捷键 Ctrl + Space:Basic code completion (the name of any class, method or va ...

  9. 外部SRAM实验,让STM32的外部SRAM操作跟内部SRAM一样(转)

    源:外部SRAM实验,让STM32的外部SRAM操作跟内部SRAM一样 前几天看到开源电子论坛(openedv.com)有人在问这个问题,我特意去做了这个实验,这样用外部SRAM就跟用内部SRAM一样 ...

  10. Tsinsen-1486:树【Trie树 + 点分治】

    暴力部分: 这个题一开始的想法是 n^2 枚举两个点,然后logn维护LCA,在倍增的同时维护异或值和 k 的个数. s_z_l老爷指导了新的思路,既然这个树只有n^2个LCA,那么枚举LCA,同时向 ...