poj2479Maximum sum【dp】

原创 2015年11月17日 20:42:40

Maximum sum
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 37038   Accepted: 11555

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.

跟poj2593一样~

#include<cstring>
#include<cstdio>
const int M=5e4+10;
int a[M], b[M], c[M];
int main(){
	
	int t;
	scanf("%d", &t);
	while(t--)
	{
		int i,j,k,m,n;
		scanf("%d", &n);
		k = 0;
		m = -10001;
		for(i = 0; i < n; i++)
		{
			scanf("%d", &a[i]);
			k += a[i];
			if(k > m)
				m = k;
			if(k < 0)
				k = 0;
			b[i] = m;		
		}
		k = 0;
		m = -10001;
		for(i = n-1; i>=0; i--)
		{
			k+=a[i];
			if(k > m)
				m = k;
			if(k < 0)
				k = 0;
			c[i] = m;		
		}
		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);
	}
}




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

相关文章推荐

POJ 2479 Maximum sum dp

POJ 2479 Maximum sum dp 最大连续子序列变形

poj2479 Maximum sum

  • 2014-11-30 12:42
  • 653B
  • 下载

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

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

POJ 2479 Maximum sum(双向DP)

1000ms,50000个数,所以每次处理的时间复杂度不能超过nlogn,否则会超时。所以要让最后扫描一次就能求出答案。 基本思路就是第一次遍历先定义2个数组,分别记录前i项和(含i)与后i项和(含i...

POJ - 2479 《Maximum sum》 【线性DP】

Description 求一个数列中两个不相交的子段和,要求和最大,输出最大值。 Input The input consists of T( Each test case contains tw...

POJ 2479 Maximum sum (DP&双最大子段和)

Maximum sum http://poj.org/problem?id=2479 Time Limit: 1000MS Memory Limit: 65536K ...

***POJ2479 Maximum sum ACM解题报告(DP难题)

这题真是个好题,非常给力的DP,哎路漫漫其修远兮,我一开始看到这题以为是区间DP,结果根本解决不了,还是百度了看了表达式,然后自己思考,这题就是求一串数中的两个子串s1和s2,并使s1+s2最大,且两...

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

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

POJ 2479 Maximum sum (2593)

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

poj 2479 Maximum sum(求最大子段和的延伸)

题目:http://poj.org/problem?id=2479 题意:        题目的大概意思是把数组分成不交两段,分别求出两段的最大子段和s1和s2,然后求出最大的s1+s...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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