poj2479

与poj2593相同

View Code
   
     
#include < iostream >
#include
< cstdio >
#include
< cstdlib >
#include
< cstring >
using namespace std;

#define maxn 100003

int f[maxn], fleft[maxn], fright[maxn];
int n;

void input()
{
for ( int i = 0 ; i < n; i ++ )
scanf(
" %d " , & f[i]);
}

void work1()
{
fleft[
0 ] = f[ 0 ];
for ( int i = 1 ; i < n; i ++ )
if (f[i] > fleft[i - 1 ] + f[i])
fleft[i]
= f[i];
else
fleft[i]
= fleft[i - 1 ] + f[i];
for ( int i = 1 ; i < n; i ++ )
fleft[i]
= max(fleft[i - 1 ], fleft[i]);
}

void work2()
{
fright[n
- 1 ] = f[n - 1 ];
for ( int i = n - 2 ; i >= 0 ; i -- )
if (f[i] > fright[i + 1 ] + f[i])
fright[i]
= f[i];
else
fright[i]
= fright[i + 1 ] + f[i];
for ( int i = n - 2 ; i >= 0 ; i -- )
fright[i]
= max(fright[i + 1 ], fright[i]);
}

void work3()
{
int ans = - 1000000000 ;

for ( int i = 0 ; i < n - 1 ; i ++ )
ans
= max(ans, fleft[i] + fright[i + 1 ]);
printf(
" %d\n " , ans);
}

int main()
{
// freopen("t.txt", "r", stdin);
int t;
scanf(
" %d " , & t);
while (t -- )
{
scanf(
" %d " , & n);
input();
work1();
work2();
work3();
}
return 0 ;
}

你可能感兴趣的:(poj)