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. 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.

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))

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <cstdio>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. const int maxn = 1e6+7, inf = -1e6+7;
  9.  
  10. int a[maxn], ls[maxn], rs[maxn], s, rst[maxn];
  11.  
  12. int main()
  13. {
  14. int t, n;
  15. scanf("%d", &t);
  16. while (t--)
  17. {
  18. s = 0;
  19. memset(ls, 0, sizeof(ls));
  20. memset(rs, 0, sizeof(rs));
  21. memset(rst, 0, sizeof(rst));
  22. scanf("%d", &n);
  23. for (int i=0; i<n; i++)
  24. scanf("%d", &a[i]);
  25. ls[0] = a[0];
  26. for (int i=1; i<n; i++) {
  27. ls[i] = max(ls[i-1]+a[i], a[i]);
  28. }
  29. rst[n-1] = rs[n-1] = a[n-1];
  30. for (int i=n-2; i>=0; i--){
  31. rs[i] = max(rs[i+1]+a[i], a[i]);
  32. rst[i] = max(rst[i+1], rs[i]);
  33. }
  34. s = inf;
  35. for (int i=0; i<n-1; i++) {
  36. s = max(s, ls[i]+rst[i+1]);
  37. }
  38. printf("%d\n", s);
  39. }
  40. return 0;
  41. }

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. 【Android Developers Training】 98. 获取联系人列表

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  2. 【Android Developers Training】 13. 支持不同平台版本

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  3. java如何LOG打印出日志信息

    log4j 记录日志方式 log4j 是apache 提供的记录日志的jar 档. 下载路径: http://logging.apache.org/log4j/1.2/download.html 这里 ...

  4. java基础(10) -线程

    线程 相当于轻量级进程,线程在程序中是独立的.并发的执行路径,每个线程有它自己的堆栈.自己的程序计数器和自己的局部变量.但是,与分隔的进程相比,进程中的线程之间的隔离程度要小.它们共享内存.文件句柄和 ...

  5. 从栈不平衡问题 理解 calling convention

    最近在开发的过程中遇到了几个很诡异的问题,造成了栈不平衡从而导致程序崩溃. 经过几经排查发现是和调用规约(calling convention)相关的问题,特此分享出来. 首先,讲一下什么是调用规约. ...

  6. Bootstrap提示框

    前面的话 提示框是一个比较常见的功能,一般来说是鼠标移动到特定元素上时,显示相关的提示语.本文将详细介绍Bootstrap提示框 基本用法 Bootstrap框架中的提示框,结构非常简单,常常使用的是 ...

  7. HttpServletResponse addHeader() 与 setHeader() 区别

    对于addHeader() 和 setHeader()这两个方法,可能我们在需要的时候,用起来感觉都一样,都能达到想要的效果.但是这两个方法本质上是不一样的.  setHeader(name, val ...

  8. Spring3中@Value注解的使用

    Spring可以通过@Value注解来直接获取properties文件里面配置的值. 1. 首先要在spring的配置文件中指明properties文件的位置: <context:propert ...

  9. jsp注册页面验证,easyui的jsp+js表单验证

    1.1下面的代码是写在Js里面的,就直接写进去不用什么其他东西,这样一个表单验证就好了(1.2图) $.extend($.fn.validatebox.defaults.rules, { phone: ...

  10. xdu_1165:均值滤波

    题目: Problem A 均值滤波问题描述图像中的噪声在空间上的自相关很低,因此往往可以用均值滤波加以削弱.具体来说,对于每个像素点P(x; y),我们选出所有距P 的Manhattan 距离不超过 ...