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.
题目描述:给出一段序列,包含n个数。在这段序列中找出两端子序列,使得这两段子序列的和最大。
题目分析:
这是一道最大子段和的延伸问题。
我们可以分别求出左往右和从右往左的最大子段和,然后再遍历一遍寻找最大值即可。
下面以从左往右为例讲一下怎么求最大子段和。
- 状态表示:
f[i] //表示1-i这一段序列中包含的最大子段和是多少
- 状态计算:可以用sum来表示当前的子段和为多少。让sum与f[i-1]进行比较,保留最大值即可。
状态转移方程为:f[i]=max(f[i-1],sum+a[i])
代码如下:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <algorithm>
#include <iomanip>
#define LL long long
const int N=5e4+5;
using namespace std;
int a[N],b[N],c[N]; //用b[]来储存从左往右的最大子段和
int main() //用c[]来储存从右往左的最大子段和
{
int t;
scanf("%d",&t); //输入数据较大,推荐使用scanf
while(t--)
{
memset(a,0,sizeof a); //先将三个数组清空
memset(b,0,sizeof b);
memset(c,0,sizeof c);
int n,ans=-1e9; //答案可能为负数,因此ans要为-1e9
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
b[0]=-1e9; //预处理,保证b[0]不会被取到
int sum=0; //当前子段和
for(int i=1;i<=n;i++)
{
b[i]=max(b[i-1],sum+a[i]); //状态转移方程
sum+=a[i];
if(sum<0) sum=0; //如果sum<0,为了保证sum子段和最大
} //要重新开始计算(即sum=0)
//同理,求从右往左的最大子段和
c[n+1]=-1e9;
sum=0;
for(int i=n;i>0;i--)
{
c[i]=max(c[i+1],sum+a[i]);
sum+=a[i];
if(sum<0) sum=0;
ans=max(ans,b[i]+c[i+1]); //遍历求最大值
}
printf("%d\n",ans);
}
return 0;
}