POJ_2479_DP
http://poj.org/problem?id=2479
从前向后保存起点到每一点的最长串,从后向前保存尾点到每一点的最长串。
然后枚举中断点,找前后和最大值就行了。
#include<iostream> #include<cstdio> #define MIN -0x3f3f3f3f; using namespace std; int a[50005],lmax[50005],rmax[50005],n; int main() { int T; scanf("%d",&T); while(T--) { scanf("%d",&n); lmax[0] = MIN; rmax[n+1] = MIN; for(int i = 1;i <= n;i++) scanf("%d",&a[i]); int sum = 0; for(int i = 1;i <= n;i++) { sum += a[i]; if(lmax[i-1] < sum) lmax[i] = sum; else lmax[i] = lmax[i-1]; if(sum < 0) sum = 0; } sum = 0; for(int i = n;i >= 1;i--) { sum += a[i]; if(rmax[i+1] < sum) rmax[i] = sum; else rmax[i] = rmax[i+1]; if(sum < 0) sum = 0; } int maxx = MIN; for(int i = 1;i < n;i++) maxx = max(maxx,lmax[i]+rmax[i+1]); printf("%d\n",maxx); } return 0; }