求一个区间内连续两段不相交区间最大和。

// File Name: 2479.cpp
// Author: Missa_Chen
// Created Time: 2013年06月22日 星期六 16时19分02秒 #include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <cstdlib> using namespace std; #define LL long long
const int inf = 0x3f3f3f3f;
const int maxn = 5e4 + 5;
int n;
int num[maxn], st[maxn], end[maxn];
int main()
{
int cas;
scanf("%d", &cas);
while (cas --)
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d", &num[i]);
for (int i = 0; i <= n + 1; ++i)
st[i] = end[i] = -inf;
int tmp = -inf, ans = -inf;
for (int i = 1; i <= n; ++i)
{
if (tmp >= 0)
tmp += num[i];
else
tmp = num[i];
end[i] = max(end[i - 1], tmp);
}
tmp = -inf;
for (int i = n; i >= 1; --i)
{
if (tmp >= 0)
tmp += num[i];
else
tmp = num[i];
st[i] = max(st[i + 1], tmp);
}
for (int i = 1; i < n; ++i)
ans = max(ans, end[i] + st[i + 1]);
printf("%d\n", ans);
}
return 0;
}

  

poj 2479 (DP)的更多相关文章

  1. poj 2479 dp求分段最大和

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38079   Accepted: 11904 Des ...

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

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

  3. (线性dp 最大连续和)POJ 2479 Maximum sum

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 44459   Accepted: 13794 Des ...

  4. POJ 2479 Maximum sum(双向DP)

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36100   Accepted: 11213 Des ...

  5. POJ 2479

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

  6. hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)

    题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...

  7. poj 1080 dp如同LCS问题

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

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

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

  9. poj 1609 dp

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

随机推荐

  1. SQL中自定义拆分为新表的函数

    /*按照符号分割字符串*/ create function [dbo].[m_split](@c varchar(2000),@split varchar(2)) returns @t table(c ...

  2. 索引服务混战ASP.NET――微软的又一个隔离墩

    话说月初笔者在华山之巅搞定了ASP.NET一起莫名奇妙的异常,自此之后和公主过着…嘟--,不好意思,书都看杂了,串了台了.好,咱们闲言少叙,书归正传. 自从上次解决了由调试文件库引起的ASP.NET执 ...

  3. Notepad++ 几款实用插件简介,让你的 Notepad++ 如虎添翼

    Notepad++ 是一款非常优秀的文本编辑器,非常适合编辑源代码.Notepad++ 安装时已经附带有 Compare 等优秀插件,通过其 Plugin Manager 可以下载更多实用插件. 一. ...

  4. oracle OVER(PARTITION BY) 函数

    OVER(PARTITION BY)函数介绍 开窗函数               Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个组返 ...

  5. WaitForTargetFPS

    WaitForTargetFPS,是关于帧数限制的,你可能开了垂直同步,其实是防止撕裂.先说撕裂,在显示器的帧缓存会被不同步的显卡的帧缓存给替换掉,导致显示器显示到一半的时候,内存被换掉,你看到上频是 ...

  6. PHP合并数组array_merge函数运算符加号与的区别

    两个的区别是:1.数组键名为数字键名时,要合并的两个数组中有同名数字KEY的时候,使用array_merge()不会覆盖掉原来的值,而使用“+”合并数组则会把最先出现的值作为最终结果返回,而把后面的数 ...

  7. Win7无法使用VPN的原因与解决方法(一)

    如果Windows 7不是通过正常安装途径的话,像Ghost错误.系统环境改变等,都有可能导致无法使用VPN,而且由于原因不同,给出的提示也不尽相同.实际上,万变不离其宗, VPN是要依靠Window ...

  8. zend studio 快捷键收集

    Ctrl + /            单行注释 Ctrl + Shift + / 添加块注释 Ctrl + Shift + \   移除块注释 Ctrl + Shift + F 调整代码格式 Ctr ...

  9. 初识IOS

    编译环境: ssh cloud@10.0.4.148  mx-cloud 1.关于ARM架构 IPHONE上的ARM-CPU架构在不断的进化过程中,目前情况如下: ARMv8 / ARM64 = iP ...

  10. HDU 1028 Ignatius and the Princess III (递归,dp)

    以下引用部分全都来自:http://blog.csdn.net/ice_crazy/article/details/7478802  Ice—Crazy的专栏 分析: HDU 1028 摘: 本题的意 ...