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

Description

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

tex=d%28A%29%3D%5Cmax_%7B1%5Cleq+s_1%5Cleq+t_1%3Cs_2%5Cleq+t_2%5Cleq+n%7D%5Cleft%5C%7B%5Csum_%7Bi%3Ds_1%7D%5E%7Bt_1%7Da_i%2B%5Csum_%7Bj%3Ds_2%7D%5E%7Bt_2%7Da_j%5Cright%5C%7D&driver=1" alt="">

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.

1000ms。50000个数,所以每次处理的时间复杂度不能超过nlogn,否则会超时。所以要让最后扫描一次就能求出答案。

基本思路就是第一次遍历先定义2个数组,分别记录前i项和(含i)与后n-i+1项和(含i)。

第二次遍历再定义2个数组。分别记录以i为终点(含i)的最大子段和与以i为起点(含i)的最大子段和。

第三次遍历再定义2个数组,分别记录第i项(含i)的之前的最大子段和与第i项(含i)的之后的最大子段和。最后遍历一遍数组求出i之前(含i)子段和与i之后(不含i)子段和的最大值就可以。

#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma commment(linker,"/STACK: 102400000 102400000")
#define lson a,b,l,mid,cur<<1
#define rson a,b,mid+1,r,cur<<1|1
using namespace std;
const double eps=1e-6;
const int MAXN=50050; int num[MAXN],n,prev[MAXN],afte[MAXN],ans1[MAXN],ans2[MAXN],fans1[MAXN],fans2[MAXN],sum; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
int tcase;
scanf("%d",&tcase);
while(tcase--)
{
scanf("%d",&n);
memset(prev,0,sizeof(prev));//前i项和(含i)
memset(afte,0,sizeof(afte));//后n-i+1项和(含i)
memset(ans1,0,sizeof(ans1));//以i为终点(含i)的最大子段和
memset(ans2,0,sizeof(ans2));//以i为起点(含i)的最大子段和
memset(fans1,0,sizeof(fans1));//第i项(含i)的之前的最大子段和
memset(fans2,0,sizeof(fans2));//第i项(含i)的之后的最大子段和
sum=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
prev[i]=prev[i-1]+num[i];
sum+=num[i];
}
if(n==2)
{
printf("%d\n",sum);
continue;
}
for(int i=n;i>=1;i--)
afte[i]=afte[i+1]+num[i];
int minn=0;
for(int i=0;i<n;i++)
{
minn=min(prev[i],minn);
ans1[i+1]=prev[i+1]-minn;
//printf("%d\n",ans1[i+1]);
}
minn=0;
for(int i=n+1;i>0;i--)
{
minn=min(afte[i],minn);
ans2[i-1]=afte[i-1]-minn;
//printf("%d\n",ans2[i-1]);
}
int maxx=-99999999;
for(int i=1;i<=n;i++)
{
maxx=max(maxx,ans1[i]);
fans1[i]=maxx;
}
maxx=-99999999;
for(int i=n;i>=1;i--)
{
maxx=max(maxx,ans2[i]);
fans2[i]=maxx;
}
int ans=-99999999;
for(int i=1;i<n;i++)
ans=max(ans,fans1[i]+fans2[i+1]);//题目规定区间不能有交集
printf("%d\n",ans);
}
return 0;
}

POJ 2479 Maximum sum(双向DP)的更多相关文章

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

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

  2. POJ 2479 Maximum sum 解题报告

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

  3. POJ #2479 - Maximum sum

    Hi, I'm back. This is a realy classic DP problem to code. 1. You have to be crystal clear about what ...

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

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

  5. 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} 即求两个子序列和的和的最大 ...

  6. [poj 2479] Maximum sum -- 转载

    转自 CSND 想看更多的解题报告: http://blog.csdn.net/wangjian8006/article/details/7870410                         ...

  7. poj 2479 Maximum sum(递推)

     题意:给定n个数,求两段连续不重叠子段的最大和. 思路非常easy.把原串划为两段.求两段的连续最大子串和之和,这里要先预处理一下,用lmax数组表示1到i的最大连续子串和,用rmax数组表示n ...

  8. URAL 1146 Maximum Sum(DP)

    Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the large ...

  9. POJ 1836 Alignment (双向DP)

    Alignment Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10804   Accepted: 3464 Descri ...

随机推荐

  1. zClip使用时ZeroClipboard生成的位置不对的问题

    zclip官网:http://steamdev.com/zclip 我之前在另外一篇博文里面写了一个解决生成的位置不对的问题,请参考:http://www.cnblogs.com/longshiyVi ...

  2. grpc(3):使用 golang 开发 grpc 服务端和client

    1,关于grpc-go golang 能够能够做grpc的服务端和client. 官网的文档: http://www.grpc.io/docs/quickstart/go.html https://g ...

  3. 倍福TwinCAT(贝福Beckhoff)基础教程7.1 TwinCAT如何简单执行NC功能块 TC2

    TC2的程序是在TC3的基础上稍作调整,只说明不同点,请先看TC3的. TC2中的一个原本是AXIS_REF类型变量被拆成了两个(PLCTONC_AXLESTRUCT和NCTOPLC_AXLESTRU ...

  4. Ubuntu(Debian)的aptitude与apt-get的区别和联系

    Ubuntu(Debian)的aptitude与apt-get的区别和联系 aptitude 与 apt-get 一样,是 Debian 及其衍生系统中功能极其强大的包管理工具.与 apt-get 不 ...

  5. 【原】使用StarUML画用例图

    在写一份升级方案的时候,发现文字描述半天,好多句子,依然不容易被人看明白,使用visio画了个流程图,后来觉得画个时序图是最清晰得了. 于是在找了一个工具: startUML,当然,做时序图,建模之类 ...

  6. javascript - 闭包之一些常见的写法

    /** * 防御型分号 */ ; (function () { //code.... console.log('一开始,我自己执行了!!!'); })(); /** * 形参 -> 实参 * 定 ...

  7. ssh之<context:component-scan base-package="com.xx" />

    <context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能, 同时还启用了注释驱动自动注入的功能 ( 即还隐式地在内部注册了 A ...

  8. Selenium2 Python 自己主动化測试实战学习笔记(五)

    7.1 自己主动化測试用例 无论是功能測试.性能測试和自己主动化測试时都须要编写測试用例,測试用例的好坏能准确的体现了測试人员的经验.能力以及对项目的深度理解. 7.1.1 手工測试用例与自己主动化測 ...

  9. poj 2601 Simple calculations

    Simple calculations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6559   Accepted: 32 ...

  10. Mysql 没有nvl()函数,却有一个类似功能的函数ifnull()

    今天自己无聊写了看了一个查询需求随手写了一个sql语句,发现竟然不能运行,MySQL报[Err] 1305 - FUNCTION ceshi.nvl does not exist的错.才意识到自己写的 ...