1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| int Max3( int A, int B, int C ) { return A > B ? A > C ? A : C : B > C ? B : C; }
int DivideAndConquer( int List[], int left, int right ) { int MaxLeftSum, MaxRightSum; int MaxLeftBorderSum, MaxRightBorderSum; int LeftBorderSum, RightBorderSum; int center, i;
if( left == right ) if( List[left] > 0 ) return List[left]; else return 0;
center = ( left + right ) / 2; MaxLeftSum = DivideAndConquer( List, left, center ); MaxRightSum = DivideAndConquer( List, center+1, right );
MaxLeftBorderSum = 0; LeftBorderSum = 0; for( i = center; i >= left; i-- ) { LeftBorderSum += List[i]; if( LeftBorderSum > MaxLeftBorderSum ) MaxLeftBorderSum = LeftBorderSum; } MaxRightBorderSum = 0; RightBorderSum = 0; for( i = center + 1; i <= right; i++ ) { RightBorderSum += List[i]; if( RightBorderSum > MaxRightBorderSum ) MaxRightBorderSum = RightBorderSum; }
return Max3( MaxLeftSum, MaxRightSum, MaxLeftBorderSum + MaxRightBorderSum ); }
int MaxSubseqSum3( int List[], int N ) { return DivideAndConquer( List, 0, N-1 ); }
|