• 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;
    }
    
    不要忘记努力,不要辜负自己 欢迎指正 QQ:1468580561
  • 相关阅读:
    java 面试每日一题2
    java 面试每日一题
    java GUI画满天星
    java JPEGImageEncoder;图像处理
    spring mvc+myBatis配置详解
    myeclipse 常用快捷键总结
    Java 如何快速序列化
    java UUID
    vue +echarts树状图
    在线预览(pptx、ppt、pps、docx、doc、xlsx、xls)
  • 原文地址:https://www.cnblogs.com/smallocean/p/10078173.html
Copyright © 2020-2023  润新知