Maximum sum
描述
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的更多相关文章
- POJ2479 Maximum sum[DP|最大子段和]
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39599 Accepted: 12370 Des ...
- ural 1146. Maximum Sum
1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...
- UVa 108 - Maximum Sum(最大连续子序列)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- 最大子矩阵和 URAL 1146 Maximum Sum
题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...
- URAL 1146 Maximum Sum(最大子矩阵的和 DP)
Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...
- ural 1146. Maximum Sum(动态规划)
1146. Maximum Sum Time limit: 1.0 second Memory limit: 64 MB Given a 2-dimensional array of positive ...
- UVa 10827 - Maximum sum on a torus
题目大意:UVa 108 - Maximum Sum的加强版,求最大子矩阵和,不过矩阵是可以循环的,矩阵到结尾时可以循环到开头.开始听纠结的,想着难道要分情况讨论吗?!就去网上搜,看到可以通过补全进行 ...
- POJ 2479 Maximum sum 解题报告
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40596 Accepted: 12663 Des ...
- Find the Maximum sum
Given an array of n elements.Find the maximum sum when the array elements will be arranged in such w ...
- [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 ...
随机推荐
- C++ 源代码到可执行代码的详细过程
编译,编译程序读取源程序(字符流),对之进行词法和语法的分析,将高级语言指令转换为功能等效的汇编代码,再由汇编程序转换为机器语言,并且按照操作系统对可执行文件格式的要求链接生成可执行程序. 源代码-- ...
- 一个较复杂的执行redis的lue脚本
- Kubernetes pod里一个特殊的容器:pause-amd64
大家在使用Docker容器或者Kubernetes时,遇到过这个容器么?gcr.io/google_containers/pause-amd64 docker ps的命令返回的结果: [root@k8 ...
- hdu-2841 Visible Trees---容斥定理
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2841 题目大意: N*M的格点上有树,从0,0点可以看到多少棵树. 解题思路: 发现如果A1/B1= ...
- Dispatch groups 与任务同步
https://developer.apple.com/library/content/documentation/General/Conceptual/ConcurrencyProgrammingG ...
- 使用ByPropertyName进行管道传输
管道参数绑定的两种方式: 可通过 help command -full查看 不同:简单讲ByPropertyName可以使B的多个参数被同时使用 使用ByPropertyName进行管道传输: 建立一 ...
- if not
if not x 在python中的意思是如果x为空 a = [] if not a: print 1 此代码会打印出1 a = [1,2,3] if not a: print 1 此代码不会打印出1 ...
- 使用jsonp获取天气情况
在这里使用的是百度天气: 整体代码如下: js: <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js ...
- C++中rapidxml用法及例子(源码)
rapidxml是一个快速的xml库,比tinyxml快了50-100倍.本文给出创建.读取.写入xml的源码. 由于新浪博客不支持文本文件上传,在使用下面代码需要先下载 rapidxml, ...
- VC++ MFC SDI/MDI Ribbon程序的停靠窗格被关闭后如何再次显示
VC++ 创建基于MFC的SDI应用程序,Visual Studio风格的主界面如下图所示,在该主界面上的视图菜单下包含有队对各个可停靠窗格显示或隐藏的控制菜单项.而基于Ribbon风格的应用程序,所 ...