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. 1
  2.  
  3. 10
  4. 1 -1 2 2 3 -3 4 -4 5 -5

Sample Output

  1. 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. poj3252 组合数学

    题目大意:给两个数字a,b求出[a,b]中转化成二进制后0的个数大于等于1的个数的数 例如1100转化成10-11,100-111,1000-1011,1100.保证每个区段都有1打头,然后有一段数字 ...

  2. hdu5391 Zball in Tina Town(威尔逊定理)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Zball in Tina Town Time Limit: 3000/1500 ...

  3. ORACLE主要的系统表和系统视图

    ORACLE主要的系统表和系统视图 1.系统表 ORACLE数据库的系统参数都存储在数据库中,可以通过SQLPLUS,以用户SYS进行查询.几个重要的表或者视图如下: v$controlfile:控制 ...

  4. JS判断请求来自Android手机还是iPhone手机,根据不同的手机跳转到不同的链接。

    <script type="text/javascript">var browser = {versions: function () {var u = navigat ...

  5. MVC 4.0 Razor模板引擎 @Html.RenderPartial 与 @Html.RenderAction 区别

    近来在学习MVC 4.0,设置布局全局网页的页脚,使用了Razor语法 @{ Html.RenderPartial("Footer", Model.FooterData); } 但 ...

  6. nodejs安装express遇到的坑

    网上很多资料都是npm install express -g; 安装完也不报错,但使用express的时候却报找不到命令: 在网上查了好多,最后用 npm install -g express-gen ...

  7. Javascript quiz

    作为一个勤劳的corder,在大年三十的前一天还留守在公司的最前线.百无聊赖中看到一套关于js的测试题,测试过后发现有些题还是有很大的意义,至少能够让我门对js基础有所重视.本人将每道题的考察点总结了 ...

  8. CCI_chapter 1

    1.1Implement an algorithm to determine if a string has all unique characters What if  you can not us ...

  9. zabbix报警把特定的应用集发送给developer

    1.创建新的action 2.创建新的develop用户 3.需要删除组不然会给组里所有人发

  10. elasticsearch 修改内存

    [elk@zjtest7-redis bin]$ cat elasticsearch.in.sh if [ "x$ES_MIN_MEM" = "x" ]; th ...