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

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.

Source

POJ Contest,Author:Mathematica@ZSU

题目大意:给定一个整数数字序列s,要求使两段不相交的子序列s1,s2的和最大

题解:动态规划

ls[i]表示以第i个元素结尾序列的最大值

rs[i]表示以第i个元素开始序列的最大值

rst[i]表示取i个元素能够达到的最大值

所以有

ls[i]=max(ls[i-1]+a[i], a[i]),  rs[i]=max(rs[i+1]+a[i], a[i])

rst[i]=max(rst(i+1), rs[i])

所以最后的答案是s=max(s, ls[i]+rst(i+1))

#include <stdio.h>
#include <string.h>
#include <cstdio>
#include <iostream> using namespace std; const int maxn = 1e6+7, inf = -1e6+7; int a[maxn], ls[maxn], rs[maxn], s, rst[maxn]; int main()
{
int t, n;
scanf("%d", &t);
while (t--)
{
s = 0;
memset(ls, 0, sizeof(ls));
memset(rs, 0, sizeof(rs));
memset(rst, 0, sizeof(rst));
scanf("%d", &n);
for (int i=0; i<n; i++)
scanf("%d", &a[i]);
ls[0] = a[0];
for (int i=1; i<n; i++) {
ls[i] = max(ls[i-1]+a[i], a[i]);
}
rst[n-1] = rs[n-1] = a[n-1];
for (int i=n-2; i>=0; i--){
rs[i] = max(rs[i+1]+a[i], a[i]);
rst[i] = max(rst[i+1], rs[i]);
}
s = inf;
for (int i=0; i<n-1; i++) {
s = max(s, ls[i]+rst[i+1]);
}
printf("%d\n", s);
}
return 0;
}

POJ 2479 Maximum sum 解题报告的更多相关文章

  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 -- 转载

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

  3. POJ 2479 Maximum sum(双向DP)

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

  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

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

  7. poj 2479 Maximum sum(递推)

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

  8. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  9. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

随机推荐

  1. dubbo 请求调用过程分析

    服务消费方发起请求 当服务的消费方引用了某远程服务,服务的应用方在spring的配置实例如下: <dubbo:referenceid="demoService"interfa ...

  2. 用超链接a来提交form表单

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  3. ASP.NET WEB API 自定义模型校验过滤器

    对外公开WEB接口时,对模型校验是常见的安全常识,常见的写法是在controller中判断ModelState.IsValid,以注册用户API为例. Model: public class Regi ...

  4. Spring (3.2.4) 常用jar 包解析

    Spring (3.2.4) 常用jar 包解析 基本jar包 spring-aop-3.2.4.RELEASE.jar spring-aspects-3.2.4.RELEASE.jar spring ...

  5. c# MySqlHelper_ExecuteSqlTran

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Text;u ...

  6. javascript运动框架(二)

    紧接着上面写的... 给div加一个边框,border:1px solid black window.onload = function(){      var div = document.getE ...

  7. VB6之切换桌面

    Desktop的API,用于切换或者系统桌面环境.扩展起来可以做一个锁屏程序或者多桌面程序. 模块部分: 'desktop.bas 'too much struct and declare unuse ...

  8. 【3D计算机图形学】变换矩阵、欧拉角、四元数

    [3D计算机图形学]变换矩阵.欧拉角.四元数 旋转矩阵.欧拉角.四元数主要用于:向量的旋转.坐标系之间的转换.角位移计算.方位的平滑插值计算.   一.变换矩阵: 首先要区分旋转矩阵和变换矩阵: 旋转 ...

  9. Docker与KVM之间的区别

    一.Docker简介 Docker 项目的目标是实现轻量级的操作系统虚拟化解决方案. Docker 的基础是 Linux 容器(LXC)等技术. 在 LXC 的基础上 Docker 进行了进一步的封装 ...

  10. Java之StringBuffer,StringBuilder,Math,Date,SimpleDateFormat,UUID,File

    java.lang 类 StringBuffer java.lang.Object java.lang.StringBuffer 所有已实现的接口: Serializable, Appendable, ...