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. 第四篇 Python循环

    While 循环 For 循环

  2. python QQ邮箱自动发送邮件

    于初学者来讲在写发送邮件代码时常见的错误有SMTPAuthenticationError535,有点懵逼,检查用户名,密码正确就是报错, 想当年笔者也是这么过来的,现在就给大家分享一下个人经验: 一, ...

  3. AcCoder Contest-115 D - Christmas

    D - Christmas Time limit : 2sec / Memory limit : 1024MB Score : 400 points Problem Statement In some ...

  4. VC++之运算符重载简单小结

    封装继承和多态是面向对象三大基本支柱.在面向对象系统中有两种编译方式:静态联编和动态联编静态联编:也叫早期联编:指系统在编译时就决定如何实现某一动作,提供了执行速度快的优点.动态联编:也叫滞后联编:指 ...

  5. 小C的记事本(栈记录字符串)

    链接:https://www.nowcoder.com/acm/contest/122/D来源:牛客网 题目描述 小C最近学会了java小程序的开发,他很开心,于是想做一个简单的记事本程序练练手. 他 ...

  6. System and Device power management.

    Advanced Configuration and Power Management Interface(ACPI)是由Intel,Microsoft等厂家订的一套Spec,规范了OS,APP对于电 ...

  7. fetch_array()与fetch_assoc()的用法

    fetch_array()与fetch_assoc()用起来没有什么大的差别,主要是怎么用?fetch_array()如果单独作为while的条件,则能够取出结果集中的所有结果.如果单独作用于结果集, ...

  8. 利用 spring 的 task:scheduled-tasks 执行定期任务

    ref是工作类 method是工作类中要执行的方法 initial-delay是任务第一次被调用前的延时,单位毫秒 fixed-delay是上一个调用完成后再次调用的延时 fixed-rate是上一个 ...

  9. SQLAlchemy技术文档(中文版)(上)

    在学习SQLAlchemy的过程中,好多时候需要查官方Tutorial,发现网上并没有完整的中文版,于是利用这两天空余时间粗略翻译了一下. 翻译效果很差....但也算是强迫自己通读一遍Tutorial ...

  10. SQL SERVER:删除筛选记录中前100条数据

    delete from table1 where id in (select top 100 id from table1)