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 ...
随机推荐
- js创建对象的几种方式 标签: javascript 2016-08-21 15:23 123人阅读 评论(0)
1.传统方法,创建一个对象,然后给这个对象创建属性和方法. var person = new Object(); person.name = "张三"; person.age = ...
- ZT-Android深入浅出之Binder机 制
转贴 不是原创! Android深入浅出之Binder机 制 一说明 Android系统最常见也是初学者最难搞明白的就是Binder了,很多很多的Service就是通过Binder机制来和客户端通讯交 ...
- hdu 6208 The Dominator of Strings【AC自动机】
hdu 6208 The Dominator of Strings[AC自动机] 求一个串包含其他所有串,找出最长串去匹配即可,但是匹配时要对走过的结点标记,不然T死QAQ,,扎心了.. #inclu ...
- sqlite3如何退出...>状态
一般是进入SQL数据语言模式了,此时要想退出...>状态,只要输入一条完整的SQL语句就行了,也就是末尾要加上:(分号)这个符号就可以退回到sqlite>状态
- Centos 7 安装Anaconda3
1.首先下载地址: https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 使用清华镜像下载速度快 2.安装 bash Anaconda3-5.1 ...
- MaBatis(5)输入/输出映射
本次全部学习内容:MyBatisLearning 输入映射: 通过parameType指定输入参数的类型,类型可以是简单类型,hashmap,pojo等 传递pojo的包装对象 需求: 即 ...
- 理解基本包装类型Number,String,Boolean
在前面我们知道了引用类型是什么了,也就能理解包装类型了.包装对象其实也是一种引用类型,之所以要单独提出来只不过是因为它们可以把原始类型的值变成(包装成)对象,这样它们也就获得了各自类型相应的特殊行为了 ...
- Jmeter关于数据库的测试(mysql数据库)
建立jdbc链接:创建JDBC Connection Configuration. 添加——配置元件——JDBC Connection configuration: 配置JDBC Connection ...
- mac无法使用80端口问题
前言:在mac os中,非root用户是无法使用小于1024的常用端口的.如果开发中需要用到80端口, 就要设置端口转发. hosts文件介绍(1)hosts文件是将域名和IP地址建立映射关系的系统文 ...
- spring加载属性(properties)文件
一.注解方式加载 jdbc.driver=org.mariadb.jdbc.Driver jdbc.url=jdbc:mariadb://localhost:3306/kt jdbc.user=roo ...