题目意思还是很好理解的,在一个数列中,找出不相交的两个子串使得其和最大。

解题思路:

  对于每个i来说,求出[0 ~ i - 1] 的最大子段和以及[i ~ n - 1]的最大子段和,在加起来,求最大的一个就行了。

  [0 ~ i - 1]的最大子段和从左向右扫描,[i ~ n - 1] 的最大子段和从右向左扫描即可。时间复杂度为 O(n)

source code:

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define ll long long
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x))) const int INF = 0x3f3f3f3f;
int a[], left[], right[];
int main(){
int i, j, t, k, n, m;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i = ; i < n; ++i) scanf("%d",&a[i]);
left[] = a[];
for(i = ; i < n; ++i){
if(left[i - ] < )
left[i] = a[i];
else
left[i] = left[i - ] + a[i];
}
for(i = ; i < n; ++i){
left[i] = Max(left[i - ], left[i]); //Max segment sum
}
right[n - ] = a[n - ];
for(i = n - ; i >= ; --i){
if(right[i + ] < )
right[i] = a[i];
else
right[i] = right[i + ] + a[i];
}
for(i = n - ; i > ; --i){
right[i] = Max(right[i + ], right[i]); //Max segment sum
}
int MAX = -INF;
for(i = ; i < n; ++i){
MAX = Max(MAX, left[i - ] + right[i]);
}
printf("%d\n",MAX);
}
return ;
}

POJ 2479 不相交最大子段和的更多相关文章

  1. 动态规划(DP),递推,最大子段和,POJ(2479,2593)

    题目链接:http://poj.org/problem?id=2479 解题报告: 1.再求left[i]的时候,先没有考虑a[i]的正负,先把a[i]放到left[i]中,然后left=max(le ...

  2. poj----Maximum sum(poj 2479)

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30704   Accepted: 9408 Desc ...

  3. POJ 2479

    ---恢复内容开始--- http://poj.org/problem?id=2479 #include <stdio.h> #include <iostream> using ...

  4. poj 2479 (DP)

    求一个区间内连续两段不相交区间最大和. // File Name: 2479.cpp // Author: Missa_Chen // Created Time: 2013年06月22日 星期六 16 ...

  5. poj 2479 Maximum sum (最大字段和的变形)

    题目链接:http://poj.org/problem?id=2479 #include<cstdio> #include<cstring> #include<iostr ...

  6. POJ 2479 Maximum sum 解题报告

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40596   Accepted: 12663 Des ...

  7. POJ - 2018 二分+单调子段和

    依然是学习分析方法的一道题 求一个长度为n的序列中的一个平均值最大且长度不小于L的子段,输出最大平均值 最值问题可二分,从而转变为判定性问题:是否存在长度大于等于L且平均值大于等于mid的字段和 每个 ...

  8. Maximum sum(poj 2479)

    题意:给一段数列,将这个数列分成两部分,使两部分的最大子段和的和最大,输出和 /* 看数据没想到是(O)n的算法,求出从前向后的最大子段和和从后向前的最大子段和, 然后枚举断点. 第一次提交不小心折在 ...

  9. To the Max POJ - 1050 (最大子段和)

    Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...

随机推荐

  1. Push segues can only be used when the.....

    刚刚遇到的两个错误,. 1, Terminating app due to uncaught exception'NSGenericException', reason: 'Push segues c ...

  2. IMP-00008: unrecognized statement in the export file: string的问题分析

    分类: Linux 上周需要将oracle10g中的某一个用户下的对象导入到oracle11g中去.用exp在10g的数据库服务器上导出的dump文件,再用imp在11g的数据库服务器上将dump文件 ...

  3. 阿里云ECS每天一件事D1:配置SSH

    近期因为项目需求,采购了两台阿里云ECS,选择的系统为CentOS 6.3 X64 安全加固版,额外买了160G的硬盘,应该够应付此项目的需求了. ECS默认已经配置好了sshd服务,可以使用root ...

  4. 人生新开始——第一天上班

    今天是我正式上班的第一天,对今天要做的工作充满期待,对将要面对的同事们也充满期待.这天起的很早,7点钟就开始睡不着了,忙着起来整理东西,看着时间还早,便打开电脑听起CNN News,练习听力,一听就听 ...

  5. HDU - 2276 Kiki &amp; Little Kiki 2

    Description There are n lights in a circle numbered from 1 to n. The left of light 1 is light n, and ...

  6. DevExpress ASP.NET 使用经验谈(9)-Dev控件客户端事件 ClientSideEvents

    上一节,已经介绍了ASPxGridView的自定义列和基本事件 ,本节接着将介绍Dev控件的客户端事件模型. 在上节示例基础上,我们增加一行菜单,使用Dev的ASPxMenu来实现,如下图所示. 图一 ...

  7. 【转】android开发中关于模拟器emulation的常见问题

    [转]android开发中关于模拟器emulation的常见问题 Trouble: 无法启动android模拟器,提示 XDM authorization key matches an existin ...

  8. 前端CSS规范大全

    一.文件规范 1.文件均归档至约定的目录中(具体要求以豆瓣的CSS规范为例进行讲解): 所有的CSS分为两大类:通用类和业务类.通用的CSS文件,放在如下目录中: 基本样式库 /css/core 通用 ...

  9. 使用RadioGroup与RadioButton实现多选一

    RadioGroup是RadioButton的集合, RadioGroup里面可以包含很多RadioButton,提供多选一机制,只能选择其中一个 RadioGroup的orientation(方向) ...

  10. 「OC」 继承

    一.基本用法 1.设计两个类Bird.Dog 1 // Bird的声明 2 @interface Bird : NSObject 3 { 4 @public 5 int weight; 6 } 7 - ...