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\n",ans);
    return 0;
}

转载于:https://www.cnblogs.com/smallocean/p/10078173.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值