描述

Given a set of n integers: A={a1, a2,…, an}, we define a function d(A) as below:

                     t1     t2
d(A) = max{ ∑ai + ∑aj | 1 <= s1 <= t1 < s2 <= t2 <= n }
i=s1 j=s2

Your task is to calculate d(A).

输入

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.

输出

Print exactly one line for each test case. The line should contain the integer d(A).

样例输入

1

10

1 -1 2 2 3 -3 4 -4 5 -5

样例输出

13

提示

In the sample, we choose {2,2,3,-3,4} and {5}, then we can get the answer.

Huge input,scanf is recommended.


大意是说在给定的数列中,选取两段不重合的子串,使得其和最大。

写在前面:一个要注特别意的问题,WA的可能就是忽略了这个问题。

|ai| <= 10000,即可能有负数,那么初始化为0就不行了,0比负数大呀!怎么办呢,要么赋一个极小值,要么初始化为数列中的开始元。

还有一个小问题,呃,也不是什么问题,就是要注意一下数组的值的清空,因为即使定义在循环内部,他的生命周期结束了,可有可能下次分配到的地址还是在那里,里面的值并没有改变。

另外,对于多个字节的类型,memset只能初始化为 0 ,-1。不信的可以试试,一个整型四个字节,他会把每个字节都的二进制的每一位都赋值为目标数字……好像是这样,应该不对,反正不能赋值为其他值


#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int data[50005],l[50005],r[50005],mid[50005];
int main() {
int t;
scanf("%d",&t);
while(t--) {
int n;
scanf("%d",&n);
for(int i = 1; i<=n; i++) {
scanf("%d",&data[i]);
}
l[1] = data[1];
for(int i = 2; i<=n; i++) {
l[i] = 0;
l[i] = max(data[i],l[i-1] + data[i]);
}
r[n] = data[n];
for(int i = n-1; i>=1; i--) {
r[i] = 0;
r[i] = max(r[i+1] + data[i],data[i]);
}
mid[n] = r[n];
for(int i = n - 1; i>=1; i--) {
mid[i] = 0;
mid[i] = max(r[i],mid[i+1]);
}
int ans = -1e9;
for(int i = 2;i<=n;i++){
ans = max(ans,l[i-1] + mid[i]);
}
printf("%d\n",ans);
}
return 0;
}

Maximum sum的更多相关文章

  1. POJ2479 Maximum sum[DP|最大子段和]

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39599   Accepted: 12370 Des ...

  2. ural 1146. Maximum Sum

    1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...

  3. UVa 108 - Maximum Sum(最大连续子序列)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  4. 最大子矩阵和 URAL 1146 Maximum Sum

    题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...

  5. URAL 1146 Maximum Sum(最大子矩阵的和 DP)

    Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...

  6. ural 1146. Maximum Sum(动态规划)

    1146. Maximum Sum Time limit: 1.0 second Memory limit: 64 MB Given a 2-dimensional array of positive ...

  7. UVa 10827 - Maximum sum on a torus

    题目大意:UVa 108 - Maximum Sum的加强版,求最大子矩阵和,不过矩阵是可以循环的,矩阵到结尾时可以循环到开头.开始听纠结的,想着难道要分情况讨论吗?!就去网上搜,看到可以通过补全进行 ...

  8. POJ 2479 Maximum sum 解题报告

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

  9. Find the Maximum sum

    Given an array of n elements.Find the maximum sum when the array elements will be arranged in such w ...

  10. [LeetCode] Maximum Sum of 3 Non-Overlapping Subarrays 三个非重叠子数组的最大和

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

随机推荐

  1. ACM HDU-2952 Counting Sheep

    Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  2. PLSQL使用绑定变量

    想对一个sql做10046trace,结果因为10g数据库无法对sql_id做,只能使用绑定变量的方法,下面sql是如何使用绑定变量运行sql的语句 declare  v_sql  VARCHAR2( ...

  3. [EffectiveC++]item37:绝不重新定义继承而来的缺省参数值

    绝不重新定义继承而来的缺省参数值 静态类型 动态类型

  4. OC基础数据类型-NSNumber

    1.NSNumber:专门用来装基础类型的对象,把整型.单精度.双精度.字符型等基础类型存储为对象 //基本数据类型 //专门用来装基础类型的对象 NSNumber * intNumber = [[N ...

  5. 推荐一个可以把网页背景色调成护眼色的Chrome扩展应用

    程序员一天有10几个小时要面对着电脑,老是这种白晃晃的屏幕,谁的眼睛受得了? 我在网上逛了一圈,找到一个比较实用的Chrome扩展应用,可以一键实现将Chrome打开网页的背景色修改成护眼的豆沙绿,这 ...

  6. 输出预测边界框,NMS非极大值抑制

    我们预测阶段时: 生成多个锚框 每个锚框预测类别和偏移量 但是,当同一个目标上可能输出较多的相似的预测边界框.我们可以移除相似的预测边界框.——NMS(非极大值抑制). 对于一个预测边界框B,模型会计 ...

  7. CPP-基础:信号量

    windows api 多线程---信号量 信号量(Semaphore)和互斥量一样,属于内核对象.它自动执行可用资源查询的测试,如果有可用资源,则可用资源的计数减少1,从而避免其它线程请求资源.当该 ...

  8. 十六、详述 IntelliJ IDEA 创建 Maven 项目及设置 java 源目录的方法

    Maven 是一个优秀的项目管理工具,它为我们提供了一个构建完整的生命周期框架.现在,就让我们一起看看如何利用 IntelliJ IDEA 快速的创建 Maven 项目吧! 如上图所示,点击Creat ...

  9. Css animation 与 float 、flex 布局问题

    1. 有这样一段css html 代码 <div class="container"> <div class="float-left"> ...

  10. HTML5零散知识点总结

    1.产生ioc图标的网站: http://www.bitbug.net/ 链接ioc图标: <link rel="shortcut icon" type="imag ...