poj-2479 Maximum sum 【最大字串和】

Maximum sum

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 34697   Accepted: 10752

Description

Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below:

Your task is to calculate d(A).

Input

The input consists of T(<=30) test cases. The number of test cases (T) is given in the first line of the 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

Print exactly one line for each test case. The line should contain the integer d(A).

Sample Input

1

10
1 -1 2 2 3 -3 4 -4 5 -5

Sample Output

13

Hint

In the sample, we choose {2,2,3,-3,4} and {5}, then we can get the answer.

Huge input,scanf is recommended.

Source

POJ Contest,Author:[email protected]

求两段的最大和,前后进行两次dp;

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<map>

using namespace std;

int t, n, p[100010], ans[100010];

int main()
{
	scanf("%d",&t);
	while (t--)
	{
		memset(ans,0,sizeof(ans));
		scanf("%d",&n);
		int b = 0, sum = -1000000000;
		for (int i = 0; i < n; i++)
		{
			scanf("%d", &p[i]);
			b += p[i];
			sum = max(b, sum);
			ans[i] = sum;
			if (b < 0)
				b = 0;
		}

		b = 0, sum = -1000000000;
		int tmp = -1000000000;

		for (int i = n - 1; i >= 1; i--)
		{
			b += p[i];
			sum = max(b, sum);
			tmp = max(tmp, ans[i - 1] + sum);
			if (b < 0)
				b = 0;
		}
		printf("%d\n",tmp);
	}
	return 0;
}
时间: 2024-08-29 12:17:21

poj-2479 Maximum sum 【最大字串和】的相关文章

[ACM] POJ 2479 Maximum sum (动态规划求不相交的两段子段和的最大值)

Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33363   Accepted: 10330 Description Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below: Your task is to calculate d(A). Input The input consists o

POJ 2479 Maximum sum(双向DP)

Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36100   Accepted: 11213 Description Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below: Your task is to calculate d(A). Input The input consists o

Poj 2479 Maximum sum【双向DP/最大连续和】

Maximum sum 题意:给定一个长度为N的数组,求两个连续的子序列,使得两个连续子序列的和最大. 分析:乍一看,跟最大连续和有点类似,但是,又有区别,因为对于这个题,考虑第i项两个连续子序列的最大和,不能仅仅由前i-1项递推得出,第i项两个连续子序列的最大和,与前i项和i以后的之间是存在关系的,因此这个题目是一个双向dp. 假如给定的序列为a0, a1, a2, a3, a4, ...... ,an,那么,对于任意第i项,我以第i个元素为分界点,用一个数组项pMax [i]来保存  区间

POJ 2479 Maximum sum

http://poj.org/problem?id=2479 题意: 给出一个整数串,求连续子串1和连续子串2,不相交并且串1加串2的和最大. 思路: 其实就是求最大连续和,题意要求就是求两段最大连续和.我们可以从左边和右边分别求最大连续和,代码中的dp_l[i]就是1~i的最大连续和,dp_r[i]则是i~n的最大连续和. 1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<

poj 2479 Maximum sum(递推)

?? 题意:给定n个数,求两段连续不重叠子段的最大和. 思路非常easy.把原串划为两段.求两段的连续最大子串和之和,这里要先预处理一下,用lmax数组表示1到i的最大连续子串和,用rmax数组表示n到i的最大连续子串和,这样将时间复杂度降为O(n). #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include&l

[poj 2479] Maximum sum -- 转载

转自 CSND 想看更多的解题报告: http://blog.csdn.net/wangjian8006/article/details/7870410                                      转载请注明出处:http://blog.csdn.net/wangjian8006 题目大意: 对于连续的整数和的串s1和s2,s1与s2不相交,使得s1+s2最大 解题方法: DP.  lt[i]代表以第i个元素结尾的串最大值  rt[i]代表以第i个元素开头的串的最大

POJ 2479 Maximum sum ( DP )

题目大意: 对整数串S,求其两个不相交的子串s1.s2,使得s1+s2的值最大. 方法:DP, lt[i]代表以第i个元素结尾的串最大值 rt[i]代表以第i个元素开头的串的最大值 那么设置一个rtm[i]代表取后i个元素之中最大连续子串的和 很显然,lt[i]=max(a[i],lt[i-1]+a[i]); rt[i]=max(a[i],rt[i+1]+a[i]); rtm[i]=max(rtm[i+1],rt[i]); #include <iostream> #include <cs

poj 2479 max sum

Maximum sumTime Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u SubmitStatus Description Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below: Your task is to calculate d(A). Input The input consists

POJ 3693 Maximum repetition substring (后缀数组)

题目大意: 求出字典序最小,重复次数最多,的子串. 思路分析: RMQ + height 数组可以求出任意两个后缀的lcp 我们枚举答案字符串的重复的长度. 如果这个字符串的长度为 l ,而且这个字符串出现过两次或两次以上 那么你会发现在原串中  str[0] str[l] str[2*l] ....肯定有相邻的两个被包含在重复的串中. 我们求出这两个相邻的后缀的lcp 我们上面仅仅说的是被包含在重复的串中,但并不一定就是以 str[0], str[l],str[2*l]....为起点的. 那我