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

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
立即现场赛了。。3个人尽然没有会dp的sad。

。

我仅仅有临阵磨枪了。
题意:给一个数列,求数列中不相交的两个子段和。要求和最大。
线性dp:线性dp的子状态与父状态一般相差一个元素,所以子问题通过加入一个增量而到达父状态。从最小的子问题到原问题。一层一层的状态转移呈现出线性递增的关系。所以称为线性dp。
题解:对于对于每一个状态i。求出[0,i-1]的最大子段和以及[i,n-1]的最大子段和 相加求最大的就可以。[0,i-1]从左往右扫描,[i,n-1]从右往左扫描。
#include <algorithm>
#include <cstdio>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 50010;
#define LL long long
int a[maxn],left[maxn],right[maxn];
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
left[0]=a[0];
for(int i=1;i<n;i++)
left[i]=left[i-1]<0?a[i]:left[i-1]+a[i];
for(int i=1;i<n;i++)
left[i]=max(left[i-1],left[i]);
right[n-1]=a[n-1];
for(int i=n-2;i>=0;i--)
right[i]=right[i+1]<0?a[i]:right[i+1]+a[i];
for(int i=n-2;i>=0;i--)
right[i]=max(right[i+1],right[i]);
int ans=-INF;
for(int i=1;i<n;i++)
ans=max(ans,left[i-1]+right[i]);
printf("%d\n",ans);
}
return 0;
}

POJ 2479-Maximum sum(线性dp)的更多相关文章

  1. POJ 2479 Maximum sum(双向DP)

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

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

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

  3. POJ 2479 Maximum sum 解题报告

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

  4. 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 ...

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

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

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

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

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

  8. poj 2479 Maximum sum(递推)

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

  9. poj 2593 Max Sequence(线性dp)

    题目链接:http://poj.org/problem?id=2593 思路分析:该问题为求给定由N个整数组成的序列,要求确定序列A的2个不相交子段,使这m个子段的最大连续子段和的和最大. 该问题与p ...

随机推荐

  1. 黑马程序猿 IO流 ByteArrayInputStream与ByteArrayOutputStream

    ---------------------- ASP.Net+Unity开发..Net培训.期待与您交流! ---------------------- package cn.itcast.IO; i ...

  2. Linux基本操作 2-----Linux文件系统基本结构

    Linux的文件系统结构             Linux文件系统为一个倒转的单根树状结构 文件系统的根为"/" 文件系统严格区分大小写 路径使用“/”来分割,在windows使 ...

  3. iOS8 Core Image In Swift:更复杂的滤镜

    iOS8 Core Image In Swift:自动改善图像以及内置滤镜的使用 iOS8 Core Image In Swift:更复杂的滤镜 iOS8 Core Image In Swift:人脸 ...

  4. iOS绘图教程

    本文是<Programming iOS5>中Drawing一章的翻译,考虑到主题完整性,翻译版本中加入了一些书中未涉及到的内容.希望本文能够对你有所帮助.(本文由海水的味道翻译整理,转载请 ...

  5. LR使用Java User协议环境报错Please add the <JDK>\bin to the path and try again

    看标题报错信息就知道,这是java编译及运行环境配置问题,运行LR脚本时,LR代理找不到java的JDK环境,当然,可能有人会遇到说,我在cmd窗口javac 环境是没问题的呀,是的,这就要看你的jd ...

  6. ASP.NET实现IE下禁用浏览器后退按钮办法

    在page_load方法里面增加如下代码: Response.Buffer = true; Response.ExpiresAbsolute = DateTime.Parse("2010-1 ...

  7. Xcode 中添加 .pch文件

    1  新建工程 2  创建  .pch文件 3   在setting里面进行设置:

  8. WCF入门教程系列二

    一.概述 WCF能够建立一个跨平台的安全.可信赖.事务性的解决方案,是一个WebService,.Net Remoting,Enterprise Service,WSE,MSMQ的并集,有一副很经典的 ...

  9. UVA 11426 GCD - Extreme (II) (欧拉函数)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Problem JGCD Extreme (II)Input: Standard ...

  10. (原)安装windows8.1和ubuntu16双系统及相互访问磁盘

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5638232.html 参考网址: http://jingyan.baidu.com/article/f ...