DS1-13
#include <stdio.h> #define MAXSIZE 10000 int Max3(int A, int B, int C); int DivideAndConquer(int List[], int left, int right); int MaxSubseqSum3(int List[], int N); int main() { int N; int List[MAXSIZE]; scanf("%d", &N); int i; for(i=0; i<N; ++i) scanf("%d", &List[i]); int ans=MaxSubseqSum3(List, N); printf("%d\n", ans); return 0; } 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) /*递归的终止条件,子列只有1个数字*/ 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); }