poj 2479 dp求分段最大和
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 38079 | Accepted: 11904 |
Description
Your task is to calculate d(A).
Input
Each test case contains two lines. The first line is an integer n(2<=n<=50000). The second line contains n integers: a1, a2, ..., an. (|ai| <= 10000).There is an empty line after each case.
Output
Sample Input
1 10
1 -1 2 2 3 -3 4 -4 5 -5
Sample Output
13
Hint
Huge input,scanf is recommended.
以i个数结尾的最大子串和,这样问题经过几步分解就变成了已经解决过的问题了。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
const int maxn=50000+100;
int a[maxn],t1[maxn],t2[maxn],l[maxn],r[maxn];
const int inf=-1000001;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
memset(t1,0,sizeof(t1));
memset(t2,0,sizeof(t2));
for(int i=1;i<=n;i++)
{
t1[i]=max(t1[i-1]+a[i],a[i]);
}
for(int i=n;i>=1;i--)
{
t2[i]=max(t2[i+1]+a[i],a[i]);
}
l[1]=a[1];
for(int i=2;i<=n;i++)
{
l[i]=max(l[i-1],t1[i]);
}
r[n]=a[n];
for(int i=n-1;i>=1;i--)
{
r[i]=max(r[i+1],t2[i]);
}
int ma=inf;
for(int i=2;i<=n;i++)
{
int t=l[i-1]+r[i];
ma=max(ma,t);
}
cout<<ma<<endl;
}
return 0;
}
poj 2479 dp求分段最大和的更多相关文章
- Poj 2096 (dp求期望 入门)
/ dp求期望的题. 题意:一个软件有s个子系统,会产生n种bug. 某人一天发现一个bug,这个bug属于某种bug,发生在某个子系统中. 求找到所有的n种bug,且每个子系统都找到bug,这样所要 ...
- POJ 2096 (dp求期望)
A - Collecting Bugs Time Limit:10000MS Memory Limit:64000KB 64bit IO Format:%I64d & %I64 ...
- poj 2479 (DP)
求一个区间内连续两段不相交区间最大和. // File Name: 2479.cpp // Author: Missa_Chen // Created Time: 2013年06月22日 星期六 16 ...
- POJ 2479 两段连续最大和
题目大意: 在一组数中,找到连续的两段 , 是这两段相加和达到最大 这里利用dp[2][N]的数组保存所有的状态 dp[0][i]表示取到第i个数时只取了一段的最大和,第i个数是一定要被取到的 dp[ ...
- POJ 3978 Primes(求范围素数个数)
POJ 3978 Primes(求范围素数个数) id=3978">http://poj.org/problem? id=3978 题意: 给你一个区间范围A和B,要你求出[A,B]内 ...
- HDU3853-LOOPS(概率DP求期望)
LOOPS Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others) Total Su ...
- HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- 浅谈关于树形dp求树的直径问题
在一个有n个节点,n-1条无向边的无向图中,求图中最远两个节点的距离,那么将这个图看做一棵无根树,要求的即是树的直径. 求树的直径主要有两种方法:树形dp和两次bfs/dfs,因为我太菜了不会写后者这 ...
- hdu4035 Maze (树上dp求期望)
dp求期望的题. 题意: 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, 从结点1出发,开始走,在每个结点i都有3种可能: 1.被杀死,回到结点1处(概率为ki) 2.找到出口,走出迷宫 ...
随机推荐
- eclipse python开发环境搭建
eclipse python开发环境搭建[非原创] 1.在www.eclipse.org官网下载Eclipse Classic 4.2.2,Win7 64位下载eclipse-SDK-4.2.2-wi ...
- MySQL学习笔记(5) - 修改和删除数据库
1.完整语句 ALTER {DATABASE | SCHEMA} [db_name] [DEFAULT] CHARACHER SET [=] charset_name; 2.修改数据库的编码方式 al ...
- Canvas -画图
2014-09-30 09:14:57 <!doctype html> <html> <head> <title> </title> < ...
- Robot FrameWork 教程链接
1. Robot Framework 教程: http://cgmblog.sinaapp.com/html/category/robot-framework 2. Robot Framework ...
- NSNotificationCenter基础知识
NSNotificationCenter基础知识 Notification的工作机制 1.对应用程序中其他地方发生的事件(比如增加一条数据库记录)感兴趣的对象,会向通告中心(Notificatio ...
- Swift—属性观察者-备
为了监听属性的变化,Swift提供了属性观察者.属性观察者能够监听存储属性的变化,即便变化前后的值相同,它们也能监听到. 属性观察者主要有以下两个: willSet:观察者在修改之前调用. didSe ...
- rsyslog的ommsql模块如何连接MYSQL的非标准数据库端口?
搞了我半小个时查找资料..最后,在一个官方文档中找到他... http://www.rsyslog.com/doc/ommysql.html Sample: The following sample ...
- CH Round #53 -【Nescafé 32】杯NOIP模拟赛
A.GCD Path http://ch.ezoj.tk/contest/CH%20Round%20%2353%20-%E3%80%90Nescaf%C3%A9%2032%E3%80%91%E6%9D ...
- sql server Convert 的函数的用法 转换成浮点数
SELECT a.ijjdid ,b.ICYDID 订单ID,b.CCYDH 订单编号,b.CWYDH 委印单号,b.CYJMC 印件名称,b.CYJLX 印件类型,b.CYWDWMC 客户名称,b. ...
- jQuery实现置顶和置底特效
原文地址:http://www.jqueryba.com/3403.html <script src="jquery.min.js" type="text/java ...