Maximum sum
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 38079   Accepted: 11904

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.

题目大意:将区间分成了两部分,让你求这两部分的和最大是多少。
思路分析:前面做过求最大连续序列和的题目,是用dp做的,状态转移方程为dp[i]=max[dp[i-1]+a[i],a[i])
这道题与那道题很像,唯一的区别就是区间变成了两部分,现在要求和最大,dp[i]的意思是以i结尾的子串的最大
和,现在可以把分成求两个dp,l[i]代表i之前的最大和,r[i]代表i之后的最大和,题目要求的不就是求l[i-1]+r[i]的
最大值么,现在问题就变成了如何求了l[i]和r[i],状态转移方程很容易确定,l[i]=max(l[i-1],t1[i]),t1[i]指的是
以i个数结尾的最大子串和,这样问题经过几步分解就变成了已经解决过的问题了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
const int maxn=50000+100;
int a[maxn],t1[maxn],t2[maxn],l[maxn],r[maxn];
const int inf=-1000001;
int main()
{
        int T;
        scanf("%d",&T);
        while(T--)
        {
            int n;
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&a[i]);
            }
            memset(t1,0,sizeof(t1));
            memset(t2,0,sizeof(t2));
            for(int i=1;i<=n;i++)
            {
                t1[i]=max(t1[i-1]+a[i],a[i]);
            }
            for(int i=n;i>=1;i--)
            {
                t2[i]=max(t2[i+1]+a[i],a[i]);
            }
            l[1]=a[1];
            for(int i=2;i<=n;i++)
            {
                l[i]=max(l[i-1],t1[i]);
            }
           r[n]=a[n];
           for(int i=n-1;i>=1;i--)
           {
               r[i]=max(r[i+1],t2[i]);
           }
           int ma=inf;
           for(int i=2;i<=n;i++)
           {
               int t=l[i-1]+r[i];
               ma=max(ma,t);
           }
           cout<<ma<<endl;
        }
     return 0;
}

poj 2479 dp求分段最大和的更多相关文章

  1. Poj 2096 (dp求期望 入门)

    / dp求期望的题. 题意:一个软件有s个子系统,会产生n种bug. 某人一天发现一个bug,这个bug属于某种bug,发生在某个子系统中. 求找到所有的n种bug,且每个子系统都找到bug,这样所要 ...

  2. POJ 2096 (dp求期望)

    A - Collecting Bugs Time Limit:10000MS     Memory Limit:64000KB     64bit IO Format:%I64d & %I64 ...

  3. poj 2479 (DP)

    求一个区间内连续两段不相交区间最大和. // File Name: 2479.cpp // Author: Missa_Chen // Created Time: 2013年06月22日 星期六 16 ...

  4. POJ 2479 两段连续最大和

    题目大意: 在一组数中,找到连续的两段 , 是这两段相加和达到最大 这里利用dp[2][N]的数组保存所有的状态 dp[0][i]表示取到第i个数时只取了一段的最大和,第i个数是一定要被取到的 dp[ ...

  5. POJ 3978 Primes(求范围素数个数)

    POJ 3978 Primes(求范围素数个数) id=3978">http://poj.org/problem? id=3978 题意: 给你一个区间范围A和B,要你求出[A,B]内 ...

  6. HDU3853-LOOPS(概率DP求期望)

    LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Su ...

  7. HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...

  8. 浅谈关于树形dp求树的直径问题

    在一个有n个节点,n-1条无向边的无向图中,求图中最远两个节点的距离,那么将这个图看做一棵无根树,要求的即是树的直径. 求树的直径主要有两种方法:树形dp和两次bfs/dfs,因为我太菜了不会写后者这 ...

  9. hdu4035 Maze (树上dp求期望)

    dp求期望的题. 题意: 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, 从结点1出发,开始走,在每个结点i都有3种可能: 1.被杀死,回到结点1处(概率为ki) 2.找到出口,走出迷宫 ...

随机推荐

  1. 最近公共祖先:LCA及其用倍增实现 +POJ1986

    Q:为什么我在有些地方看到的是最小公共祖先? A:最小公共祖先是LCA(Least Common Ancestor)的英文直译,最小公共祖先与最近公共祖先只是叫法不同. Q:什么是最近公共祖先(LCA ...

  2. POJ 3273 Monthly Expense(二分查找+边界条件)

    POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...

  3. 使用HTML5中的Canves标签制作时钟特效

    <!DOCTYPE html > <html> <head> </head> <body> <canvas id="cloc ...

  4. Readonly and other things about C++

    1. in c# readonly can be delayed to initialize in constructor. 2. in c++ totally no readonly. Many p ...

  5. javascript 数组slice和splice

    var a = [1,4,2,5,6,9,10];console.log(a.slice(3)); //[5,6,9,10]console.log(a.slice(-3)); //[6,9,10]co ...

  6. 自制单片机之八……USB-ISP下载线

    现在的笔记本包括台式机都渐渐地舍弃了并口.串口:很多网友也跟我说,台式没有并口了,下载线没法用了,让我帮他想想办法.看来做个USB-ISP下载线是势在必行了. 在网上搜了下,主要有两种方案,一种是用F ...

  7. 价格更低、SLA 更强的全新 Azure SQL 数据库服务等级将于 9 月正式发布

    继上周公告之后,很高兴向大家宣布更多好消息,作为我们更广泛的数据平台的一部分, 我们将在 Azure 上提供丰富的在线数据服务.9 月,我们将针对 Azure SQL 数据库推出新的服务等级.Azur ...

  8. linux下,FTP服务相关

    虚拟机上安装完CentOS 6.5后,使用FTP工具(FlashFXP)来上传文件到虚拟机的linux,结果发现连接不上.现在解决了,解决方法总结一下: 1.先检查有没有安装ftp.好像包括两个部分, ...

  9. java_内存划分

    内存划分

  10. 手动同步chrome浏览器

    chrome浏览器每次设置好的标签在重新开机后都会变回设置前的状态,崩溃,每次设置好后还是手动同步一下吧. 1. 点击 工具(右上角的三个点)-->设置 2. 点击 高级同步设置 3. 点击 使 ...