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. QT编程之——使用全局变量

    在Qt中使用全局变量的实例 1.首先须要在一个头文件里对全局了变量使用exern方法进行定义. //g.h extern char *testStr; 2.仅仅能在cpp文件里对事实上例化,而且实例化 ...

  2. Python3.6学习笔记(五)

    网络编程 网络程序出现的比互联网要早很多,实现方式主要依靠网络上不同主机间进程的通信,通信协议最重要的是TCP/IP协议.在这两个协议基础上还有很多更高级的协议,包括HTTP.SMTP等.要进行两个主 ...

  3. select case when if 的一些用法

    概述:sql语句中的case语句与高级语言中的switch语句,是标准sql的语法,适用于一个条件判断有多种值的情况下分别执行不同的操作. 首先,让我们看一下CASE的语法.在一般的SELECT中,其 ...

  4. 字符串转换atof atoi atol gcvt strtod strtol strto ul toascii tolower toupper

    atof(将字符串转换成浮点型数) 相关函数 atoi,atol,strtod,strtol,strtoul 表头文件 #include <stdlib.h> 定义函数 double at ...

  5. 学习笔记:状态压缩DP

    我们知道,用DP解决一个问题的时候很重要的一环就是状态的表示,一般来说,一个数组即可保存状态.但是有这样的一些题 目,它们具有DP问题的特性,但是状态中所包含的信息过多,如果要用数组来保存状态的话需要 ...

  6. flink和spark stream等框架的对比

    参考这篇文章: https://www.sohu.com/a/196257023_470008 我们当时的目标就是要设计一款低延迟.exactly once.流和批统一的,能够支撑足够大体量的复杂计算 ...

  7. Android高效加载大图、多图解决方案,有效避免程序内存溢出现象

    好久没有写博客了,今天就先写一个小的关于在Android中加载大图如何避免内存溢出的问题. 后面会写如何使用缓存技术的核心类,android.support.v4.util.LruCache来加载图片 ...

  8. iOS开发-Get请求,Post请求,同步请求和异步请求

    标题中的Get和Post是请求的两种方式,同步和异步属于实现的方法,Get方式有同步和异步两种方法,Post同理也有两种.稍微有点Web知识的,对Get和Post应该不会陌生,常说的请求处理响应,基本 ...

  9. 拍案惊奇!9款神奇的jQuery/CSS3经典插件

    款非常给力的jQuery/CSS3经典插件,插件包括CSS3图片特效.jQuery动画菜单.jQuery时尚登录表单等,一起来看看这些jQuery插件. .CSS3图片重力感应特效 这是一款应用重力感 ...

  10. 编写和执行C#代码的插件:CS-Script for Notepad++

    这个插件可以方便得让您在Notepad++编辑器中编辑和执行C#代码(脚本).它具备通用的C#智能感知和项目任务管理功能,方式非常类似于MS Visual Studio.除了这一点,它提供了通用的调试 ...