题目大意:给一个数列,求出数列中不相交的2个子段和,求和最大。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <functional>
#include <vector>
#include <algorithm>
using namespace std;
typedef __int64 LL;
const int maxn = 50005;
const int inf = 2000000001;
int n;
int num[maxn],ld[maxn],rd[maxn];
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
int cas;
scanf("%d",&cas);
while( cas -- )
{
scanf("%d",&n);
for( int i = 1; i <= n; i ++ )
scanf("%d",&num[i]);
ld[1] = num[1];
for( int i = 2; i <= n; i ++ )
{
if( ld[i-1] < 0 )
ld[i] = num[i];
else
ld[i] = ld[i-1] + num[i];
}
for( int i = 2; i <= n; i ++ )
ld[i] = max( ld[i-1],ld[i] );
rd[n] = num[n];
for( int i = n-1; i >= 1; i -- )
{
if( rd[i+1] < 0 )
rd[i] = num[i];
else
rd[i] = rd[i+1] + num[i];
}
for( int i = n-1; i >= 1; i -- )
rd[i] = max( rd[i+1],rd[i] );
int ans = -inf;
for( int i = 2; i <= n; i ++ )
ans = max( ans,ld[i-1] + rd[i] );
printf("%d\n",ans);
}
return 0;
}