Equal Cut

Posted smallocean

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Equal Cut相关的知识,希望对你有一定的参考价值。

Equal Cut

题目描述

Snuke has an integer sequence A of length N.

He will make three cuts in A and divide it into four (non-empty) contiguous subsequences B,C,D and E. The positions of the cuts can be freely chosen.

Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively. Snuke is happier when the absolute difference of the maximum and the minimum among P,Q,R,S is smaller. Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.

Constraints
4≤N≤2×105
1≤Ai≤109
All values in input are integers.

输入

Input is given from Standard Input in the following format:

N
A1 A2 … AN

输出

Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.

题解

这题首先想到的是枚举,枚举一个中间节点,然后再分别枚举前后两个节点…..

明显会超时,所以不能简单的暴力

然后想想会不会有很多重复的部分

从1到i,然后从i+1到n,每次都这样,就有很多重复的计算

定义l和r指针,如果移动使两个区间的差值减小,那就移动

暴力枚举i

现在问题是这么写会不会覆盖到所有情况

答案是肯定的

代码:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=2e5+7;
ll s[maxn];
const ll inf=1e17;
ll sum[maxn];
inline ll get(int l,int r){
    //if(l>r) return inf;
    return sum[r]-sum[l-1];
}
int main(){
    int n;scanf("%d",&n);
    for (register int i = 1; i <=n ; ++i) {
        scanf("%lld",&s[i]);
        sum[i]=s[i]+sum[i-1];
    }
    int l=1,r=3;
    ll a,b,c,d;
    ll maxx,minn;
    ll ans=inf;
    for(register int i = 2;i<n-1;++i){
       while(l<=(i-2)&&abs(get(1,l)-get(l+1,i))>=abs(get(1,l+1)-get(l+2,i))){
           l++;
       }
        while(r<=(n-2)&&abs(get(i+1,r)-get(r+1,n))>=abs(get(i+1,r+1)-get(r+2,n))){
            r++;
        }
        a=get(1,l);
        b=get(l+1,i);
        c=get(i+1,r);
        d=get(r+1,n);
        maxx=max(max(a,b),max(c,d));
        minn=min(min(a,b),min(c,d));
        ans=min(ans,maxx-minn);
    }
    printf("%lld
",ans);
    return 0;
}

以上是关于Equal Cut的主要内容,如果未能解决你的问题,请参考以下文章

100D Equal Cut

ARC100 D - Equal Cut

AtCoder Regular Contest 100 (ARC100) D - Equal Cut 二分

leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段

非常简短的片段:PHP word cut

Final Cut Pro X中的音视频片段如何自由拖动?