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. LVS工作模式与调度算法

    LVS三种工作模式.十种调度算法介绍 工作模式介绍: 1.Virtual server via NAT(VS-NAT) 优点:集群中的物理服务器可以使用任何支持TCP/IP操作系统,物理服务器可以分配 ...

  2. Android各种Manager

    一.PowerManager 主要是用来控制电源状态,设置屏幕状态,和电池待机状态 PowerManager  pm = ((PowerManager)getSystemService(POWER_S ...

  3. Unity 游戏框架搭建 (一) 概述

      为了重构手头的一款项目,翻出来当时未接触Unity时候收藏的视频<Unity项目架构设计与开发管理>,对于我这种初学者来说全是干货.简单的总结了一下,以后慢慢提炼. 关于Unity的架 ...

  4. 一个Ruby静态代码分析器 rubocop

    A Ruby static code analyzer, based on the community Ruby style guide. http://rubocop.readthedocs.io ...

  5. 阿里云服务器 通过JavaMail发送邮箱STMP问题( 25端口被禁用 使用SSL协议465端口 )

    我们传统使用的比较简单的是 STMP 25端口收发邮件 今天发现刚购买的阿里云服务器不能作为客户端通过STMP 25端口发送邮件 开始在网上有说发现是JDK1.8的原因,然后自己也把JDK1.8换到了 ...

  6. Hotspot JVM垃圾回收器

    前两篇<JVM入门——运行时数据区><JVM常见垃圾回收算法>所提到的实际上JVM规范以及常用的垃圾回收算法,具体的JVM实现实际上不止一种,有JRockit.J9等待,当然最 ...

  7. Java 日志框架终极教程

    概述 对于现代的 Java 应用程序来说,只要被部署到真实的生产环境,其日志的重要性就是不言而喻的,很难想象没有任何日志记录功能的应用程序被运行于生产环境中.日志 API 所能提供的功能是多种多样的, ...

  8. C++第一篇--类的引入

    C++第一篇--类的引入 1. 用C语言输出两个人的信息 Person1.c:通过字符实现 #include <stdio.h> int main(int argc,int **argv) ...

  9. neo4j 数据库导入导出

    工作中需要将 A 图数据库的数据完全导出,并插入到 B 图数据库中.查找资料,好多都是通过导入,导出 CSV 文件来实现.然而,经过仔细研究发现,导出的节点/关系 都带有 id 属性 ,因为 A B ...

  10. multipath多路径实验02-配置多路径软件

    multipath多路径实验02-配置多路径软件 在上一篇文章<multipath多路径实验01-构建iSCSI模拟环境>,我构建了iSCSI的模拟环境,在文章最后,已经成功配置并在主机上 ...