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 ...
随机推荐
- WPF编程学习——样式
本文目录 1.引言 2.怎样使用样式? 3.内联样式 4.已命名样式 5.元素类型样式 6.编程控制样式 7.触发器 1.引言 样式(Style),主要是用来让元素或内容呈现一定外观的属性.WPF中的 ...
- [译]如何使用 Docker 组件开发 Django 项目?
原文地址:Django Development With Docker Compose and Machine 以下为译文 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包 ...
- Win 7英文系统显示中文乱码的解决(转)
Win 7英文系统显示中文乱码的解决http://www.enet.com.cn/article/2011/0811/A20110811896633.shtml 请点击Startmenu并点击Cont ...
- FZU Problem 2148 Moon Game (判断凸四边形)
题目链接 题意 : 给你n个点,判断能形成多少个凸四边形. 思路 :如果形成凹四边形的话,说明一个点在另外三个点连成的三角形内部,这样,只要判断这个内部的点与另外三个点中每两个点相连组成的三个三角形的 ...
- win7桌面便签。自带的
新建WIN7下的桌面便签小程序 桌面—>新建 快捷方式-> 输入%windir%\system32\StikyNot.exe
- 【LA3942-Remember the word 】Trie
http://acm.hust.edu.cn/vjudge/problem/22109 题意:给定n个单词,一个字符串,问把这个字符串划分为若干个单词的连接(单词可重复使用)有多少种方案(mod200 ...
- openvswitch 修改dpid(datapath id)
版本: $ sudo ovs-vsctl -Vovs-vsctl (Open vSwitch) 2.0.2Compiled May 13 2015 18:49:53 $ sudo ovs-vsctl ...
- IOS中表视图(UITableView)使用详解
IOS中UITableView使用总结 一.初始化方法 - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)styl ...
- iOS开发推送--客户端 服务端
1.推送过程简介 (1)App启动过程中,使用UIApplication::registerForRemoteNotificationTypes函数与苹果的APNS服务器通信,发出注册远程推送的申请. ...
- iOS & Mac 调试命令(VMMap&Top)
vmmap & top命令解析内存 Is there any way to query the address space of a process on Mac? On Windows, w ...