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. 不支持placeholder浏览器下对placeholder进行处理

    if(document.createElement('input').placeholder !== '') { $('[placeholder]').focus(function() { var i ...

  2. MySQL访问控制实现原理

    MySQL访问控制实现原理 MySQL 访问控制实际上由两个功能模块共同组成,从第一篇的第二章架构组成中可以看到,一个是负责 “看守 MySQL 大门”的用户管理模块,另一个就是负责监控来访者每一个动 ...

  3. Kafka 客户端实现逻辑分析

    这里主要分析kafka 客户端实现 (代码分析以perl kafka实现为准) kafka客户端分为生产者和消费者,生产者发送消息,消费者获取消息. 在kafka协议里客户端通信中用到的最多的四个协议 ...

  4. Javacript 学习笔记

    一.初探 javacript 学习无法是围绕着对象和属性两个方面来兜圈子,万变不离其宗. 在js中,能点出来的,或者中括号里面的必然是属性(方法).数组除外. 对象调用属性! 对象调用属性! 对象调用 ...

  5. (转载)Oracle10g 数据泵导出命令 expdp 使用总结(二)

    原文链接:http://hi.baidu.com/edeed/item/2c454cff5c559f773d198b94 Oracle10g 数据泵导出命令 expdp 使用总结(一) 1.1.2 e ...

  6. Wireshark网络端点和会话

    如果想让网络进行正常通信,你必须至少拥有两台设备进行数据流交互.端点(endpoint)就是指网络上能够发送和接受数据的一台设备.举例来说,在TCP/IP的通信中就有两个断电:接收和发送数据系统的IP ...

  7. Chrome浏览器扩展开发系列之十四:本地消息机制Native messaging

    通过将浏览器所在客户端的本地应用注册为Chrome浏览器扩展的“本地消息主机(native messaging host)”,Chrome浏览器扩展还可以与客户端本地应用之间收发消息. 客户端的本地应 ...

  8. C#获取数据库连接字符

    有两种用法:1)using System.Configuraiton; string ConStr=ConfigurationManager.ConnectionStrings["ConSt ...

  9. 使用three.js加载3dmax资源,以及实现场景中的阴影效果

    使用three.js可以方便的让我们在网页中做出各种不同的3D效果.如果希望2D绘图内容,建议使用canvas来进行.但很多小伙伴不清楚到底如何为我们绘制和导入的图形添加阴影效果,更是不清楚到底如何导 ...

  10. js实现图片旋转、模板文件查看图片大图之记录篇[二]

    一个小小的前端需求送给大家,使用js实现图片旋转,并且点击图片能够实现规定格式的大图. 主要使用的是jQuery的delegate()方法实现图片旋转,该方法主要的功能就是给某个组件绑定一个或一组事件 ...