除了输入格式,这题与POJ-2479完全一样,一次AC。
1 #include<cstdio> 2 3 #define MAXN 100010 4 5 #define maxx(x,y) ((x)>(y))?(x):(y) 6 7 int a[MAXN]; 8 int b1[MAXN], b2[MAXN]; 9 int c1[MAXN], c2[MAXN]; 10 11 int main(void) 12 { 13 int N, S; 14 scanf("%d", &N); 15 while(N > 0){ 16 for(int i = 0; i < N; ++i) scanf("%d", &a[i]); 17 b1[0] = a[0]; 18 c1[0] = a[0]; 19 for(int i = 1; i < N; ++i){ 20 b1[i] = (b1[i-1] > 0) ? (b1[i-1] + a[i]) : a[i]; 21 c1[i] = maxx(c1[i-1], b1[i]); 22 } 23 b2[N-1] = a[N-1]; 24 c2[N-1] = a[N-1]; 25 for(int i = N-2; i >= 0; --i){ 26 b2[i] = (b2[i+1] > 0) ? (b2[i+1] + a[i]) : a[i]; 27 c2[i] = maxx(c2[i+1], b2[i]); 28 } 29 S = c1[0] + c2[1]; 30 for(int i = 2; i < N; ++i) 31 S = maxx(S, (c1[i-1] + c2[i])); 32 printf("%d\n", S); 33 scanf("%d", &N); 34 } 35 return 0; 36 }
时间: 2024-10-19 09:11:26