描述

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. Java 基本IO操作

    1.基本IO操作     有时候我们编写的程序除了自身会定义一些数据信息外,还需要引用外界的数据,或是将自身的数据发送到外界,这时我们需要使用输入与输出. 1)输入与输出       输入:是一个从外 ...

  2. 美国加拿大著名公共知识分子简·雅各布斯 (Jane Jacobs)的5本书

    作者是美国.加拿大著名公共知识分子.最近看了她的三本书<城市经济><城市与国家财富><经济的本质>.出版社把这三本书归入“城市-经济”三部曲,三本书英文原版的出版日 ...

  3. February 14 2017 Week 7 Tuesday

    Love lives in cottages as well as in courts. 爱情无贵贱,贫富皆有之. Many people, especially boys, complain tha ...

  4. python UI自动化实战记录四:测试页面1-pageobject

    该部分记录测试页面1-IndexPage,所有首页上的元素定位.操作.获取属性等方法都写在该类中. 1 首页类继承自BasePage 2 首页类第一部分写的是所有的定位器 3 首页类第二部分类的方法, ...

  5. 使用ViewPager实现Tab

    一.效果演示及分析 我们直接看两幅图.如下:                   上两幅图实现的效果就是: (1)手指左右滑动时,中间的布局呈现不同的效果.而且下面的按钮也会做相应的改变. (2)我们 ...

  6. UVa 1395 - Slim Span(最小生成树变形)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. Perl 修改文件内容

    把test.txt文件中的字符aaa替换成bbb perl -pi -e "s/aaa/bbb/gi" test.txt 把test.txt文件中的字符aaa替换成bbb,并生成一 ...

  8. Volecity模板引擎学习笔记

    转自:https://blog.csdn.net/reggergdsg/article/details/50937433 最近项目中用到了volecity模板,这里做一下笔记,学习中...相比较 Fr ...

  9. Motrix 全平台多功能下载工具[Windows、macOS、Linux]

    Motrix的界面很朴素,使用起来其实也很方便.点击左侧的「+」图标就可以添加下载任务,跟其他的下载工具没什么区别.暂停.恢复.查看.复制,这些功能都不缺.界面也挺好看的,下载速度还会提示在图标的角标 ...

  10. C++ 全局变量不明确与 using namespace std 冲突

    写了个汉诺塔,使用全局变量count来记录步数,结果Error:count不明确 #include <iostream> using namespace std; ; void hanoi ...