poj-2479http://acm.pku.edu.cn/JudgeOnline/problem?id=2479


#include
<
stdio.h
>
#include < stdlib.h >
#include < string .h >
long s1[ 50001 ],s2[ 50001 ],max;
int a[ 50001 ],n,t;
int main(){
int i,j,i1;
scanf( " %d " , & t);
for (i1 = 1 ;i1 <= t;i1 ++ )
{
s1[ 0 ] = 0 ;s2[ 0 ] = 0 ;
scanf( " %d " , & n);
for (i = 1 ;i <= n;i ++ )
scanf( " %d " , & a[i]);
s1[ 1 ] = a[ 1 ];max = a[ 1 ];
for (i = 2 ;i <= n;i ++ )
if (s1[i - 1 ] + a[i] < a[i])s1[i] = a[i];
else s1[i] = s1[i - 1 ] + a[i];
for (i = 2 ;i <= n;i ++ )
{
if (max > s1[i])s1[i] = max;
max = s1[i];
}
s2[n] = a[n];
max = a[n];
for (i = n - 1 ;i >= 1 ;i -- )
if (s2[i + 1 ] + a[i] < a[i])s2[i] = a[i];
else s2[i] = s2[i + 1 ] + a[i];
for (i = n - 1 ;i >= 1 ;i -- )
{
if (max > s2[i])s2[i] = max;
max = s2[i];
}
max = s1[ 1 ] + s2[ 2 ];
for (i = 1 ;i <= n - 1 ;i ++ )
if (max < (s1[i] + s2[i + 1 ]))
max = s1[i] + s2[i + 1 ];
printf( " %ld\n " ,max);
}
return 0 ;
}
#include < stdlib.h >
#include < string .h >
long s1[ 50001 ],s2[ 50001 ],max;
int a[ 50001 ],n,t;
int main(){
int i,j,i1;
scanf( " %d " , & t);
for (i1 = 1 ;i1 <= t;i1 ++ )
{
s1[ 0 ] = 0 ;s2[ 0 ] = 0 ;
scanf( " %d " , & n);
for (i = 1 ;i <= n;i ++ )
scanf( " %d " , & a[i]);
s1[ 1 ] = a[ 1 ];max = a[ 1 ];
for (i = 2 ;i <= n;i ++ )
if (s1[i - 1 ] + a[i] < a[i])s1[i] = a[i];
else s1[i] = s1[i - 1 ] + a[i];
for (i = 2 ;i <= n;i ++ )
{
if (max > s1[i])s1[i] = max;
max = s1[i];
}
s2[n] = a[n];
max = a[n];
for (i = n - 1 ;i >= 1 ;i -- )
if (s2[i + 1 ] + a[i] < a[i])s2[i] = a[i];
else s2[i] = s2[i + 1 ] + a[i];
for (i = n - 1 ;i >= 1 ;i -- )
{
if (max > s2[i])s2[i] = max;
max = s2[i];
}
max = s1[ 1 ] + s2[ 2 ];
for (i = 1 ;i <= n - 1 ;i ++ )
if (max < (s1[i] + s2[i + 1 ]))
max = s1[i] + s2[i + 1 ];
printf( " %ld\n " ,max);
}
return 0 ;
}
s1[]表示从1到i的最大子序列和中的最大值,s2[i]表示从n到i的最大子序列和中的最大值。最初时s1[i ]表示从1开
到i的最大子序列和,s2[ ]表示从n开始向前到i的最大子序列和。然后再用max对两数组进行了处理。最终所要的答案
是s1[i]+s2[i+1]中最大的一个。此题同poj-2593,是hdu-1003的强化版.
poj-2593http://acm.pku.edu.cn/JudgeOnline/problem?id=2593


1
#include
<
stdio.h
>
2 #include < stdlib.h >
3 #include < string .h >
4 long s1[ 100500 ],s2[ 100500 ],max;
5 int a[ 100500 ],n;
6 int main(){
7 int i,j;
8 scanf( " %d " , & n);
9
10 while (n != 0 )
11 {
12 for (i = 1 ;i <= n;i ++ )
13 scanf( " %d " , & a[i]);
14 s1[ 1 ] = a[ 1 ];
15 max = a[ 1 ];
16 for (i = 2 ;i <= n;i ++ )
17 if (s1[i - 1 ] + a[i] < a[i])s1[i] = a[i];
18 else s1[i] = s1[i - 1 ] + a[i];
19
20 for (i = 2 ;i <= n;i ++ )
21 {
22 if (max > s1[i])s1[i] = max;
23 max = s1[i];
24 }
25
26 s2[n] = a[n];
27 max = a[n];
28
29 for (i = n - 1 ;i >= 1 ;i -- )
30 if (s2[i + 1 ] + a[i] < a[i])s2[i] = a[i];
31 else s2[i] = s2[i + 1 ] + a[i];
32
33 for (i = n - 1 ;i >= 1 ;i -- )
34 {
35 if (max > s2[i])s2[i] = max;
36 max = s2[i];
37 }
38
39 max = s1[ 1 ] + s2[ 2 ];
40 for (i = 2 ;i <= n - 1 ;i ++ )
41 if (max < (s1[i] + s2[i + 1 ]))
42 max = s1[i] + s2[i + 1 ];
43
44 printf( " %ld\n " ,max);
45 scanf( " %d " , & n);
46 }
47 return 0;
48 }
2 #include < stdlib.h >
3 #include < string .h >
4 long s1[ 100500 ],s2[ 100500 ],max;
5 int a[ 100500 ],n;
6 int main(){
7 int i,j;
8 scanf( " %d " , & n);
9
10 while (n != 0 )
11 {
12 for (i = 1 ;i <= n;i ++ )
13 scanf( " %d " , & a[i]);
14 s1[ 1 ] = a[ 1 ];
15 max = a[ 1 ];
16 for (i = 2 ;i <= n;i ++ )
17 if (s1[i - 1 ] + a[i] < a[i])s1[i] = a[i];
18 else s1[i] = s1[i - 1 ] + a[i];
19
20 for (i = 2 ;i <= n;i ++ )
21 {
22 if (max > s1[i])s1[i] = max;
23 max = s1[i];
24 }
25
26 s2[n] = a[n];
27 max = a[n];
28
29 for (i = n - 1 ;i >= 1 ;i -- )
30 if (s2[i + 1 ] + a[i] < a[i])s2[i] = a[i];
31 else s2[i] = s2[i + 1 ] + a[i];
32
33 for (i = n - 1 ;i >= 1 ;i -- )
34 {
35 if (max > s2[i])s2[i] = max;
36 max = s2[i];
37 }
38
39 max = s1[ 1 ] + s2[ 2 ];
40 for (i = 2 ;i <= n - 1 ;i ++ )
41 if (max < (s1[i] + s2[i + 1 ]))
42 max = s1[i] + s2[i + 1 ];
43
44 printf( " %ld\n " ,max);
45 scanf( " %d " , & n);
46 }
47 return 0;
48 }