poj 2593Max Sequence【dp】

原创 2015年11月17日 20:41:18

Max Sequence
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 16666   Accepted: 6980

Description

Give you N integers a1, a2 ... aN (|ai| <=1000, 1 <= i <= N). 

You should output S. 

Input

The input will consist of several test cases. For each test case, one integer N (2 <= N <= 100000) is given in the first line. Second line contains N integers. The input is terminated by a single line with N = 0.

Output

For each test of the input, print a line containing S.

Sample Input

5
-5 9 -5 11 20
0

Sample Output

40

#include<cstring>
#include<cstdio>
#include<cstdlib>
const int M = 1e5+20;
int a[M], b[M], c[M];
int main()
{
	int n;
	while(scanf("%d", &n)==1, n)
	{
		int i, j, k;
		int r, t;
		r = 0;
		t = -1001;
		for(i = 0; i < n; i++)
		{
			scanf("%d", &a[i]);
			r += a[i];
			if(r > t)
				t = r;
			if(r < 0)
				r = 0;
			b[i] = t;		
		}
		r = 0;
		t = -1001;
		for(i = n-1; i >= 0; i--)
		{
			r += a[i];
			if(r > t)
				t = r;
			if(r < 0)
				r = 0;
			c[i] = t;		
		}
		int mx = b[0]+c[1];
		for(i = 0; i < n-1; i++)
		{
			if(mx < b[i]+c[i+1])
				mx = b[i]+c[i+1];
		}
		printf("%d\n", mx);
	} 
	return 0;
} 



版权声明:转载请注明出处:http://blog.csdn.net/liu6886

相关文章推荐

DP专题3 POJ 2593 Max Sequence

传送门:http://poj.org/problem?id=2593                    ...

POJ 2479 Maximum sum && 2593 Max Sequence (dp)买一送一

Max Sequence Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16511   Accepted: 69...

poj2593 Max Sequence dp

Language: Default Max Sequence Time Limit: 3000MS   Memory Limit: 65536K Total S...

POJ 2593 Max Sequence POJ 2479 Maximum sum (线性dp 最大连续两不重叠子段和)

Max Sequence Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 15836 Accepted: 66...

[ACM] POJ 2593 Max Sequence (动态规划,最大字段和)

Max Sequence Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 15569   ...

poj 2593 max sequence

/* busyfisher 2013/6/25 poj 2593 max sequence 动态规划:分别求出从左到右顺序数组的最大子数组和从右到左顺序数组的最大子数组; ans = ma...

poj 2479 Maximum sum poj 2593 Max Sequence

题目链接http://poj.org/problem?id=2479 题目大意:给定数组a求数组a中两个大子序列之和的最大值 思路:对a数组进行扫描求出以左端点开始到右端点的最大子序列之和 再次扫描求...

POJ2479&&POJ2593 Maximum sum&&Max Sequence(最大连续和)

题意: 一组数列,分成两组,求怎么分使两组分别的连续最大和加起来最大,输出总和 要点: 最大连续和的一个简单变形,先从左往右求一次dp,将对应的当前最大值存储下来。然后从右往左dp一次,计算最大...

【动态规划】poj2479 Maximum sum && poj2593 Max Sequence(求两段不相交字串的最大和)

两道题都是求两段不相交的子串最大和 2479: Description Given a set of n integers: A={a1, a2,..., an}, we define a fun...

POJ 2479 Maximum sum && POJ 2593 Max Sequence

DP(求两个子序列和的最大值)
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)