题意:
给你一个数组
a
a
a,你要将其分成四份,让这四份中和的最大值
−
-
−最小值最小,输出这个最小值。
n
≤
2
e
5
,
a
i
≤
1
e
9
n\le2e5,a_i\le1e9
n≤2e5,ai≤1e9
思路:
直接枚举不是很好做,考虑先枚举中间的位置 i i i,设被划分成的四段和分别是 s 1 , s 2 , s 3 , s 4 s1,s2,s3,s4 s1,s2,s3,s4且 s 1 < s 2 , s 3 < s 4 s1<s2,s3<s4 s1<s2,s3<s4,那么答案就是 m a x ( s 2 , s 4 ) − m i n ( s 1 , s 3 ) max(s2,s4)-min(s1,s3) max(s2,s4)−min(s1,s3)。由此可知,我们要让 a b s ( s 2 − s 1 ) abs(s2-s1) abs(s2−s1)和 a b s ( s 4 − s 3 ) abs(s4-s3) abs(s4−s3)都尽可能小,用两个指针维护一下就好啦。
// Problem: D - Equal Cut
// Contest: AtCoder - AtCoder Regular Contest 100
// URL: https://atcoder.jp/contests/arc100/tasks/arc100_b
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const LL inf=0x3f3f3f3f3f3f3f3f;
const double eps=1e-6;
int n;
int a[N];
LL pre[N];
LL get(LL a,LL b,LL c,LL d) {
return max(a,max(b,max(c,d)))-min(a,min(b,min(c,d)));
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++) scanf("%d",&a[i]),pre[i]=pre[i-1]+a[i];
LL ans=inf;
for(int l=0,r=1,i=1;i<=n;i++) {
while(l+1<=i&&abs(pre[l]-pre[i]+pre[l])>abs(pre[l+1]-pre[i]+pre[l+1])) l++;
while(r+1<=n&&abs(pre[r]-pre[i]-pre[n]+pre[r])>abs(pre[r+1]-pre[i]-pre[n]+pre[r+1])) r++;
ans=min(ans,get(pre[l],pre[i]-pre[l],pre[r]-pre[i],pre[n]-pre[r]));
}
printf("%lld\n",ans);
return 0;
}
/*
*/