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

解题思路:

  对于每个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. [LeetCode]题解(python):084-Largest Rectangle in Histogram

    题目来源: https://leetcode.com/problems/largest-rectangle-in-histogram/ 题意分析: 给定一个数组,数组的数字代表这个位置上的bar的高度 ...

  2. (Problem 72)Counting fractions

    Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...

  3. BootStrap 轮播 Carousel

    参考 http://wrongwaycn.github.io/bootstrap/docs/javascript.html#collapse 同样 启动方式有2种 一种是在div的class中加  另 ...

  4. 微软Xbox360 E与微软Xbox360 slim Kinect套装(1TB)哪个好

    原文地址:http://product.pchome.net/digi_home_playstation_microsoft_xbox360slimkinect1tb/381793.html 微软Xb ...

  5. 在Spring Boot启动后执行指定代码

    在开发时有时候需要在整个应用开始运行时执行一些特定代码,比如初始化环境,准备测试数据等等. 在Spring中可以通过ApplicationListener来实现相关的功能,不过在配合Spring Bo ...

  6. OpenCV之Python学习笔记

    OpenCV之Python学习笔记 直都在用Python+OpenCV做一些算法的原型.本来想留下发布一些文章的,可是整理一下就有点无奈了,都是写零散不成系统的小片段.现在看 到一本国外的新书< ...

  7. 基于Visual C++2013拆解世界五百强面试题--题13-找最大公共子字符串

    编程实现:找出两个字符串中最大公共子字符串,如"abccade"和"dgcadde"的最大子字符串为"cad". 如果不考虑效率的话直接比较 ...

  8. android AChartEngine源代码

    昨天翻自己曾经下过的apache开源project项目,看到一个AChartEnginee看了一下自带的Demo才意识到这个东西的强大.立刻想把源代码down一份,在CSDN上有人挂5分让人下载,实在 ...

  9. NeHe OpenGL lession 4

    // lession4.c #include <OpenGL/OpenGL.h> #include <GLUT/GLUT.h> #include <stdio.h> ...

  10. 在CheckBox中,仅仅允许选择一项

    作用相当于RadioButonList <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=" ...