POJ 2593&&2479:Max Sequence

Max Sequence

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 16329   Accepted: 6848

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

题意是给出一个序列,求这个序列中两个子序列的和的最大值。

两三年前切了POJ2479,但当时还很不理解dp (当然现在对dp的理解程度也就能切切dp水题。。。)。所以做这道题的时候无限感慨。

其实求一个序列的和的最大值很简单,即dp[i]=max(dp[i-1]+value[i], value[i])

现在它要求两个序列的和的最大值。所以想到从左边来一次,从右边来一次。

left[i]表示从第1个数字到当前第i个数字为止,左边的最大序列和。

right[i]表示从第Test个数字(从右向左)到第i个数字为止,右边的最大序列和。

代码:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int left_v[100005];
int right_v[100005];
int value[100005];

int main()
{
	int Test;
	while(cin>>Test)
	{
		if(!Test)
			break;

		left_v[0]=0;
		right_v[0]=0;
		left_v[Test+1]=0;
		right_v[Test+1]=0;

		int i,max_v=-100000000;
		for(i=1;i<=Test;i++)
		{
			cin>>value[i];
		}
		left_v[1]=value[1];
		right_v[Test]=value[Test];

		for(i=2;i<=Test;i++)
		{
			left_v[i]=max(left_v[i-1]+value[i],value[i]);
		}
		for(i=Test-1;i>=1;i--)
		{
			right_v[i]=max(right_v[i+1]+value[i],value[i]);
		}
		for(i=2;i<=Test;i++)
		{
			left_v[i]=max(left_v[i-1],left_v[i]);
		}
		for(i=Test-1;i>=1;i--)
		{
			right_v[i]=max(right_v[i+1],right_v[i]);
		}
		for(i=1;i<Test;i++)
		{
			if(left_v[i]+right_v[i+1]>max_v)
				max_v=left_v[i]+right_v[i+1];
		}
		cout<<max_v<<endl;
	}
	return 0;
}

自己把这道题A掉,相当开心。2015/7/5。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-27 00:31:09

POJ 2593&&2479:Max Sequence的相关文章

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

Max Sequence Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 15569   Accepted: 6538 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. Fo

poj2593 Max Sequence(求两个不相交最大字段和)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=2593 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 e

hdu 1081 &amp; poj 1050 To The Max(最大和的子矩阵)

转载请注明出处:http://blog.csdn.net/u012860063 Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum

HDU - 1003 - Max Sum &amp;&amp; POJ - 1050 - To the Max (经典DP问题)

题目传送:HDU - 1003 思路:最大子序列和 dp[i]= a[i]   (dp[i-1]<0) dp[i]= dp[i-1]+a[i]   (dp[i-1]>=0) AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include

POJ 1050 To the Max DP题解

一维最大字段和的扩展. 要诀是固定列的左右点,比如左边记录为left, 右边记录为right,那么一个循环left从0到COL,行最大值,那么right从left开始循环到COl,就可以考虑到所有列组合了,这个循环是O(n*n),然后求范围列内的行最大子段和,时间是O(n), 这样巧妙地把二维的问题转化为一维了,最终时间复杂度是O(n^3). 可以参考Geeks上的讲解,不过他的最大子段和代码写的挺挫的,我的代码会简洁很多,而且也考虑到了负值情况了. Geeks地址:http://www.gee

HDU 1003:Max Sum(DP)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 142742    Accepted Submission(s): 33225 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s

POJ 1050 To the Max(DP,最大子矩阵和)

POJ 1050 题意:给一个矩阵,求出元素和最大的子矩阵. 思路: 之前曾写过最大子串和的一篇文章,这次由一维上升到了二维. 我们可以通过累加每行相同列或每列相同行的值,将其储存在一个数组中,便可以将二维降至一维. 时间复杂度为O(n^3). 参考: 累加每一行相同列的做法(java实现) 累加每一列相同行的做法(C++实现) code: /* *Author : Flint_x *Created Time : 2015-07-23 15:10:01 *File name : POJ1050.

POJ-2593 Max Sequence

Max Sequence Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16001   Accepted: 6715 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. Fo

每日算法之四十二:Permutation Sequence (顺序排列第k个序列)

The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312" "