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. Dom4j解析xml格式的字符串【java】

    一般我们会使用dom4j.SAX.w3c来解析xml文件,网上也大多提供此类解决方案. 但在实际项目中,也有会解析xml格式的字符串报文的. 比如,有如下字符串: String = "< ...

  2. DreamWeaver文件保存时,提示"发生共享违例"问题的解决方法

    在学习牛腩老师的JS视频中,视频中的例子要求实现一个是23个3相乘的结果,在用Dreamweaver制作时,, <script language="javascript" t ...

  3. Android应用程序启动过程源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6689748 前文简要介绍了Android应用程 ...

  4. STL中map,set的基本用法示例

    本文主要是使用了STL中德map和set两个容器,使用了它们本身的一些功能函数(包括迭代器),介绍了它们的基本使用方式,是一个使用熟悉的过程. map的基本使用: #include "std ...

  5. Java中的编码格式

    Java中的编码 gbk编码 中文占用2个字节,英文占1个字节; utf-8编码 中文占用3个字节.,英文占用1个字节; Java是双字节编码 (utf-16be) utf -16be 中文占2个字节 ...

  6. jquery 底部导航透明度变化

    如果滚动条到达底部,footer-nav 的透明度为1,否则为0.8: html <div class="footer-nav" id="footer"& ...

  7. WIN7系统JavaEE(java+tomcat7+Eclipse)环境配

    在进行 Java Web环境开发之前,首先要做的第一件事就是搭建开发环境,开发环境搭建成功,接下来便是对整个开发环境进行测试,可以通过编写一个简单的JSP 程序发布到Tomcat应用服务器上运行. 1 ...

  8. IE iframe 中 js 的 cookie 读写不到的解决办法

    1.看这里(改服务器配置) http://www.cr173.com/html/16696_1.html 2.使用object模拟iframe,不使用iframe框架 <html> < ...

  9. MongoDb笔记(一)

    1.Mongodb 数据库是动态生成的可以使用use 数据库名 来指定要使用的数据库,如果数据库不存在就自动生成一个 2.插入一个文档:db.foo.insert({"name": ...

  10. ios变量的property属性设置和意义

    IOS 的@property和@synthesize帮我们轻易的生成对象的getter和setter方法来完成对对象的赋值和访问.但是如果我们如果要动态设置对象的getter和setter方法可以使用 ...