Maximum sum
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 30704   Accepted: 9408

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
题意:
给一个数列,求出数列中不相交的两个字段和,要求和最大。
思路:
对每一个i来说,求出【0~i-1】的最大子段和以及【i~n-1】的最大子段和,再加起来求最大的一个就行了。[0~i-1]的最大子段和从左向右扫描,【i~n-1】从右想左扫描
即可,时间复杂度O(n).
代码:
 #include<iostream>
#include<cstdio>
#define maxn 50001
#include<algorithm>
using namespace std;
int a[maxn];
int left[maxn];
int right[maxn];
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int t,i;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(i=;i<n;i++)
scanf("%d",&a[i]);
//此时::left【i】为包涵i最大字段和
::left[]=a[];
for( i=; i<n;i++)
if(::left[i-]<)
::left[i]=a[i];
else
::left[i]=::left[i-]+a[i];
//此时left[i]为i左边最大字段和
for(i=; i<n;i++)
::left[i]=max(::left[i],::left[i-]);
::right[n-]=a[n-];
for(i=n-;i>=;i--)
{
if(::right[i+]<)
::right[i]=a[i];
else
::right[i]=::right[i+]+a[i];
}
for(i=n-;i>=;i--)
::right[i]=max(::right[i+],::right[i]);
int res=-;
for(i=;i<n;i++)
{
res=max(res,::left[i-]+::right[i]);
}
printf("%d\n",res);
}
return ;
}

poj----Maximum sum(poj 2479)的更多相关文章

  1. POJ 2479 Maximum sum POJ 2593 Max Sequence

    d(A) = max{sum(a[s1]..a[t1]) + sum(a[s2]..a[t2]) | 1<=s1<=t1<s2<=t2<=n} 即求两个子序列和的和的最大 ...

  2. POJ 2479 Maximum sum 解题报告

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40596   Accepted: 12663 Des ...

  3. poj 2479 Maximum sum (最大字段和的变形)

    题目链接:http://poj.org/problem?id=2479 #include<cstdio> #include<cstring> #include<iostr ...

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

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

  5. POJ 2479 Maximum sum(双向DP)

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

  6. ACM:POJ 2739 Sum of Consecutive Prime Numbers-素数打表-尺取法

    POJ 2739 Sum of Consecutive Prime Numbers Time Limit:1000MS     Memory Limit:65536KB     64bit IO Fo ...

  7. POJ.2739 Sum of Consecutive Prime Numbers(水)

    POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...

  8. POJ 2739 Sum of Consecutive Prime Numbers(素数)

    POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...

  9. POJ 2479-Maximum sum(线性dp)

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33918   Accepted: 10504 Des ...

随机推荐

  1. hue解决timed out(code THRIFTSOCKET):None

    报错栈: Traceback (most recent call last): File , in decorator return func(*args, **kwargs) File , in e ...

  2. 《深入理解C指针》

    <深入理解C指针> 基本信息 原书名:Understanding and using C pointers 作者: (美)Richard Reese 译者: 陈晓亮 丛书名: 图灵程序设计 ...

  3. C# 指南

    C# 指南提供了许多有关 C# 语言的资源. 此网站面向许多不同的受众群体. 你可能希望探索本指南的不同部分,具体视你的编程经验或 C# 语言和 .NET 使用经验而定. 对于从未接触过编程的开发者: ...

  4. 【BestCoder】【Round#41】

    枚举+组合数?+DP+数学问题 http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=582 QAQ许久没打过比赛,来一发BC,结果还是只 ...

  5. VC++ 改动VMware BIOS、uuid_location、ethernet0_address等

    VC++ 改动VMware BIOS.uuid_location.ethernet0_address等.主要问题例如以下 (1)随机产生16进制数. (2)改动vmx相应项.依据规则一般仅仅改动最后三 ...

  6. 混沌数学之Kent模型

    相关软件:混沌数学之离散点集图形DEMO 相关代码: // http://wenku.baidu.com/view/7c6f4a000740be1e650e9a75.html // 肯特映射 clas ...

  7. 【转】一种新型的Web缓存欺骗攻击技术

    为了减少WEB响应时延并减小WEB服务器负担,现在WEB缓存技术已经用的非常普遍了,除了专门的CDN,负载均衡以及反向代理现在也会缓存一部分的网页内容.这里我要介绍一种WEB缓存欺骗攻击技术,这种攻击 ...

  8. 基于MINA实现server端心跳检测(KeepAliveFilter)

    MINA自带了对心跳协议的支持,可以对心跳做出细致的配置,本文在次基础上实现了server端对client端的心跳检测. 在开始之前先简单介绍下keepAlive的机制: 首先,需要搞清楚TCP ke ...

  9. 谈谈javascript的函数作用域

    在一些类似c语言的编程语言中,花括号内的每一段代码都具有各自的作用域,而且变量在声明他们的代码段之外是不可见的,我们称为块级作用域(block scope),而javascript中没有块级作用域.取 ...

  10. JS操作JSON常用方法

    一.JSON字符串的替换 工作经常遇到这样的字符串,如下: 需要经过替换后,才能从字符串转化成JSON对象.这里我们需要用JS实现replaceAll的功能, 将所有的 ' \\" ' 替换 ...