Maximum sum
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 37035   Accepted: 11551

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

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

Sample Output

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
 
题意:原串分成两个不相交连续子序列,求最大和
const int maxn = ;

int num[maxn];
int le[maxn], ri[maxn]; int main() { int T;
scanf("%d", &T);
while (T --) {
memset(le, , sizeof(le));
memset(ri, , sizeof(ri));
int n;
scanf("%d", &n);
for (int i = ; i <= n; i ++) scanf("%d", &num[i]);
le[] = num[];
ri[n] = num[n];
int cur_sum = num[] < ? : num[];
for (int i = ; i <= n; i ++) {
cur_sum += num[i];
le[i] = max(le[i-], cur_sum);
if (cur_sum < ) cur_sum = ;
}
cur_sum = num[n] < ? : num[n];
for (int i = n - ; i >= ; i --) {
cur_sum += num[i];
ri[i] = max(ri[i+], cur_sum);
if (cur_sum < ) cur_sum = ;
}
int ans = -0x3fffffff;
for (int i = ; i <= n; i ++) {
ans = max(ans, le[i-] + ri[i]);
}
printf("%d\n", ans);
} return ;
}

POJ2479(最长连续子序列和)的更多相关文章

  1. TOJ 5065: 最长连续子序列

    5065: 最长连续子序列   Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal Submit: 140   ...

  2. 【TOJ 5065】最长连续子序列(前缀和)

    Description 给定一系列非负整数,求最长的连续子序列,使其和是7的倍数. Input 第一行为正整数N(1<=N<=50000),接下来有N行,每行有一个非负整数,所有整数不大于 ...

  3. 最长连续子序列 Longest Consecutive Sequence

    2018-11-25 16:28:09 问题描述: 问题求解: 方法一.如果不要求是线性时间的话,其实可以很直观的先排序在遍历一遍就可以得到答案,但是这里明确要求是O(n)的时间复杂度,那么就给了一个 ...

  4. HDU1231 最长连续子序列

    最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  5. leetcode 128. 最长连续子序列

    题目描述: 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入:[100, 4, 200, 1, 3, 2] 输出:4 即最长的连续序列为 [1,2, ...

  6. hdu1231 最长连续子序列和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1231 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, N ...

  7. 最长连续子序列(dp,分而治之递归)

    5227: 最大子列和问题 时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte 总提交: 76            测试通过:46 描述 给定KK个整数组 ...

  8. HDU 3308 线段树单点更新+区间查找最长连续子序列

    LCIS                                                              Time Limit: 6000/2000 MS (Java/Oth ...

  9. BNUOJ 4215 最长公共连续子序列

    最长公共连续子序列 Time Limit: 1000ms Memory Limit: 65536KB   64-bit integer IO format: %lld      Java class ...

随机推荐

  1. 今日Linux下安装部署禅道

    我的linux系统是在虚拟机上安装的Ubuntu,禅道在官网www.zentao.net下载安装的开源版的linux64位,采用一键安装包安装.安装前要求:系统上不能有自己安装的mysql .下载的安 ...

  2. Ajax请求被缓存的几种处理方式

    Ajax请求被缓存的几种处理方式 我们都知道IE会针对ajax请求的地址缓存请求结果,直到缓存过期之前,针对相同地址发出的请求,只有第一次会请求会真正发送到服务端.在某种情况下,这种缓存机制确实能提高 ...

  3. LeetCode 全解(bug free 训练)

    1.Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...

  4. mongodb数据库操作之简单查询

    1. 2. 3.修改器 默认一条一条修改 4. 5.查询 6.mysql简单操作

  5. BZOJ 4004 JLOI2015 装备购买 高斯消元+线性基

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4004 Description 脸哥最近在玩一款神奇的游戏,这个游戏里有 n 件装备,每件装 ...

  6. Collections常用方法总结

    public class CollectionsTest { public static void main(String[] args) { List<Integer> list = n ...

  7. 编程练习:寻找发帖"水王"

    题目: 寻找发帖"水王" 来源: 编程之美 分析 衍生:就是给定一个数组,其中某个元素出现次数超过了数组长度的一半,找出这个元素 方法s 方法1 对这个串进行遍历,同时对出现的元素 ...

  8. HL7 2.6 解析(XML)

    用途:检验化验(LIS)实验室设备数据交换解析. using System; using System.Collections.Generic; using System.Text; using Sy ...

  9. Phaser的timer用法

    1. 延迟timer,相当于setTimeout game.time.events.add(Phaser.Timer.SECOND*5,this.delayOver,this); 2. 循环timer ...

  10. 如何使用Photoshop制作真实的尺子

    前言: 日常生活中经常性的偶尔需要测量一些东西的尺寸,但刚好手头上缺乏尺子等必要的测量工具,这时候其实我们可以利用Photoshop,临时制作一把基于现实物理单位(如:厘米)的虚拟尺子. 难点: 像素 ...