poj2479:Maximum sum
Description
Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below:
Your task is to calculate d(A).
Input
The input consists of T(<=30) test cases. The number of test cases (T) is given in the first line of the input.
Each test case contains two lines. The first line is an integer n(2<=n<=50000). The second line contains n integers: a1, a2, ..., an. (|ai| <= 10000).There is an empty line after each case.
Each test case contains two lines. The first line is an integer n(2<=n<=50000). The second line contains n integers: a1, a2, ..., an. (|ai| <= 10000).There is an empty line after each case.
Output
Print exactly one line for each test case. The line should contain the integer d(A).
Sample Input
1 10 1 -1 2 2 3 -3 4 -4 5 -5
Sample Output
13
Hint
In the sample, we choose {2,2,3,-3,4} and {5}, then we can get the answer.
Huge input,scanf is recommended.
Huge input,scanf is recommended.
题解
寻找最大连续子段和,因为求两个所以正反向各一遍,再遍历分界点。
先求到当前位置的最大子段和(包含该位置),再求当前位置及之前的最大子段和(可包含可不包含)
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 #define maxn 50005 6 #define inf 1<<29 7 int f[maxn],d[maxn],a[maxn],lf[maxn],rf[maxn]; 8 int ans; 9 int main() 10 { 11 int T,n; 12 scanf("%d",&T); 13 while(T--) 14 { 15 scanf("%d",&n); 16 ans=-inf; 17 for(int i=1 ; i<=n ; ++i ) 18 scanf("%d",&a[i]); 19 f[0]=f[n+1]=d[0]=d[n+1]=lf[0]=lf[n+1]=rf[0]=rf[n+1]=-inf; 20 for(int i=1 ; i<=n ; ++i) 21 f[i]=max(a[i],f[i-1]+a[i]); 22 for(int i=1 ; i<=n ; ++i) 23 lf[i]=max(lf[i-1],f[i]); 24 for(int i=n ; i>=1 ; --i) 25 d[i]=max(a[i],d[i+1]+a[i]); 26 for(int i=n ; i>=1 ; --i) 27 rf[i]=max(rf[i+1],d[i]); 28 for(int i=1 ; i<n ; ++i ) 29 ans=max(ans,lf[i]+rf[i+1]); 30 printf("%d\n",ans); 31 } 32 return 0; 33 }