# 蓝桥杯：2019年第十届蓝桥杯省赛B组试题J — 灵能传输

24 篇文章 4 订阅

#### 4.求解

#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;
}


#### END

• 26
点赞
• 54
收藏
觉得还不错? 一键收藏
• 7
评论
03-25 808
01-26 3299
10-03 438
03-24 940
08-13 359
03-27 1155

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。