solution.cpp 1.4 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#include <algorithm>
#include <cstring>
#include <iostream>
#include <limits.h>

using namespace std;

typedef long long LL;
const int N = 300010;

int n;
LL sum[N], a[N], s0, sn;
bool st[N];

int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d", &n);
        sum[0] = 0;
        for (int i = 1; i <= n; i++)
        {
            scanf("%lld", &sum[i]);
            sum[i] += sum[i - 1];
        }
        s0 = sum[0], sn = sum[n];
        if (s0 > sn)
            swap(s0, sn);
        sort(sum, sum + n + 1);
        for (int i = 0; i <= n; i++)
            if (s0 == sum[i])
            {
                s0 = i;
                break;
            }
        for (int i = n; i >= 0; i--)
            if (sn == sum[i])
            {
                sn = i;
                break;
            }
        memset(st, 0, sizeof st);
        int l = 0, r = n;
        for (int i = s0; i >= 0; i -= 2)
        {
            a[l++] = sum[i];
            st[i] = true;
        }
        for (int i = sn; i <= n; i += 2)
        {
            a[r--] = sum[i];
            st[i] = true;
        }
        for (int i = 0; i <= n; i++)
            if (!st[i])
            {
                a[l++] = sum[i];
            }
        LL res = 0;
        for (int i = 1; i <= n; i++)
            res = max(res, abs(a[i] - a[i - 1]));
        printf("%d\n", res);
    }
    return 0;
}