POJ:Maximum sum

总时间限制: 
1000ms
 
内存限制: 
65536kB
描述
Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below:
                     t1     t2 
d(A) = max{ ∑ai + ∑aj | 1 <= s1 <= t1 < s2 <= t2 <= n }
i=s1 j=s2

Your task is to calculate d(A).
输入
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.
输出
Print exactly one line for each test case. The line should contain the integer d(A).
样例输入
1

10
1 -1 2 2 3 -3 4 -4 5 -5
样例输出
13
提示
In the sample, we choose {2,2,3,-3,4} and {5}, then we can get the answer.

Huge input,scanf is recommended.
来源
POJ Contest,Author:Mathematica@ZSU
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<cmath>
#include<algorithm>
const int INF = 0x3f3f3f3f;
using namespace std;


int main()
{
    int T, n, data[50005];
    int dp1[50005], dp2[50005];
    int kk1[50005], kk2[50005];

    scanf("%d", &T);
    while (T--){
        scanf("%d", &n);
        for (int i = 1; i <= n; i++){
            scanf("%d", &data[i]);
        }

        memset(dp1, 0, sizeof dp1);
        memset(dp1, 0, sizeof dp2);
        kk1[0] = -INF, kk2[n + 1] = -INF;
        for (int i = 1; i <= n; i++){
            dp1[i] = max(dp1[i - 1] + data[i], data[i]);
            dp2[n - i + 1] = max(dp2[n - i + 2] + data[n - i + 1], data[n - i + 1]);
            kk1[i] = max(kk1[i - 1], dp1[i]);
            kk2[n - i + 1] = max(kk2[n - i + 2], dp2[n - i + 1]);
        }
        int ans = -INF;
        for (int i = 1; i <= n - 1; i++){
            ans = max(ans, kk1[i] + kk2[i + 1]);
        }
        printf("%d\n", ans);

    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zhouyuepku/p/10061646.html