注意清空函数和绝对值函数(abs)的用法还有判重数组的用处,以及要注意结果和所需变量的数据类型,注意不要溢出!!!

alt

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5;
long long a[N], s[N];
bool vis[N];
int main()
{
    int T; scanf("%d", &T);
    while (T--)
    {
        memset(vis, 0, sizeof(vis));
        int n; scanf("%d", &n);
        s[0] = 0;
        for (int i = 1; i <= n; i++)
        {
            cin>>s[i];
            s[i] += s[i - 1];//计算前缀和
        }
    long long s0 = 0, sn = s[n];//这俩前缀和是不变的
    if (s0 > sn)
        swap(s0, sn);
    sort(s, s + n + 1);
    int l = 0,r = n;
    for (int i = lower_bound(s, s + n + 1, s0) - s; i >= 0; i -= 2)
        //图中的路线1: 从s0到min。隔一个数取一个
        a[l++] = s[i], vis[i] = 1;
        for (int i = lower_bound(s, s + n + 1, sn) - s; i <= n; i += 2)
            //图中的路线3:  从max到sn。隔一个数取一个
            a[r--] = s[i], vis[i] = 1;
        for (int i = 0; i <= n; i++)//图中的路线2 从min到max
            if (!vis[i])
                a[l++] = s[i];
        long long res = 0;
        for (int i = 1; i <= n; i++)
            res = max(res, abs(a[i] - a[i - 1]));
        cout << res << endl;
    }
    return 0;
}