poj 2479 (DP)
求一个区间内连续两段不相交区间最大和。
// 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)的更多相关文章
- poj 2479 dp求分段最大和
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38079 Accepted: 11904 Des ...
- 动态规划(DP),递推,最大子段和,POJ(2479,2593)
题目链接:http://poj.org/problem?id=2479 解题报告: 1.再求left[i]的时候,先没有考虑a[i]的正负,先把a[i]放到left[i]中,然后left=max(le ...
- (线性dp 最大连续和)POJ 2479 Maximum sum
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 44459 Accepted: 13794 Des ...
- POJ 2479 Maximum sum(双向DP)
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 36100 Accepted: 11213 Des ...
- POJ 2479
---恢复内容开始--- http://poj.org/problem?id=2479 #include <stdio.h> #include <iostream> using ...
- hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)
题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...
- poj 1080 dp如同LCS问题
题目链接:http://poj.org/problem?id=1080 #include<cstdio> #include<cstring> #include<algor ...
- poj 2479 Maximum sum (最大字段和的变形)
题目链接:http://poj.org/problem?id=2479 #include<cstdio> #include<cstring> #include<iostr ...
- poj 1609 dp
题目链接:http://poj.org/problem?id=1609 #include <cstdio> #include <cstring> #include <io ...
随机推荐
- 2014 ACM/ICPC Asia Regional Xi'an Online Paint Pearls
传说的SB DP: 题目 Problem Description Lee has a string of n pearls. In the beginning, all the pearls have ...
- hadoop-ha QJM架构应用故障总结
部署hadoop-ha QJM架构过程我就不说了,参考 我的博客:hadoop-ha QJM架构部署故障一: namenode 报错日志如下: WARN org.apache.hadoop.hd ...
- const int *p与int *const p的区别(转:csdn,suer0101)
本文只是一篇学习笔记,是看了<彻底搞定C指针>中的相关篇幅后的一点总结,仅此而已! 一.先搞清const int *p与int const *p的区别 它们的区别就是:没有区别!! 无论谁 ...
- myeclipse 10的破解以及运行run.bat错误或者双击立即消失的问题
安装包下载: ed2k://|file|[myeclipse.10.0.更新发布].myeclipse-10.0-offline-installer-windows.exe|947752488|73b ...
- 2015年4月 非常干货之Python资源大全
[非常干货之Python资源大全]Python是一门美妙的语言,简单易用,容易提升.它是一门主流语言,却没有其它专业语言的弊病,从游戏,ML,GUI到科学和数学有着巨量的函数库. 直击现场 <H ...
- 处理HTTP状态码
1.1.4 处理HTTP状态码 上一节介绍HttpClient访问Web资源的时候,涉及HTTP状态码.比如下面这条语句: int statusCode=httpClient.executeMeth ...
- (转)价值240万的photoshop中文教程,错过了后悔都来不及 (吹得好响)
PS抠图方法 一.魔术棒法——最直观的方法 适用范围:图像和背景色色差明显,背景色单一,图像边界清晰. 方法意图:通过删除背景色来获取图像. 方法缺陷:对散乱的毛发没有用. 使用方法:1.点击“魔 ...
- DFS/BFS+思维 HDOJ 5325 Crazy Bobo
题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...
- UML系列02之UML类图(1)
类图介绍 类图,是UML(统一建模语言)中用于描述"类"以及"类与类"之间关系的示意图.它形象的描述出了系统的结构,帮助人们理解系统.类图是在"所有的 ...
- SSIS ->> Data Flow Design And Tuning
Requirements: Source and destination system impact Processing time windows and performance Destinati ...