zoukankan      html  css  js  c++  java
  • 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.

    样例输入

    5
    3 2 4 1 2
    

    样例输出

    2
    

    提示

    If we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3. Here, the maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute difference of 2. We cannot make the absolute difference of the maximum and the minimum less than 2, so the answer is 2.

     
    //枚举中点位置 再根据中点位置 贪心l,r的位置 代码如下 参考:https://blog.csdn.net/aaakirito/article/details/80884168?utm_source=blogxgwz5
    #include <bits/stdc++.h>
    
    using namespace std;
    typedef long long ll;
    inline ll read(){
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    const int maxn = 200005;
    const ll inf = 0x7fffffff;
    ll a[maxn];
    ll sum[maxn];
    int main()
    {
       // cout<<inf<<endl;
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            a[i]=read();
            sum[i]+=sum[i-1]+a[i];
        }
        int l=1,r=3;
        ll minn = inf;
        for(int i=2;i<n-1;i++)
        {
            while(l<i&&abs((sum[i]-sum[l])-(sum[l]-sum[0]))>=abs((sum[i]-sum[l+1])-(sum[l+1]-sum[0])))
            {
                l++;
            }
            while(r<n&&abs((sum[r]-sum[i])-(sum[n]-sum[r]))>abs((sum[r+1]-sum[i])-(sum[n]-sum[r+1])))
            {
                r++;
            }
            ll x,y,p,q;
            x=sum[i]-sum[l];
            y=sum[l]-sum[0];
            p=sum[r]-sum[i];
            q=sum[n]-sum[r];
            minn = min(minn,max(x,max(p,max(y,q)))-min(x,min(y,min(p,q))));
        }
        printf("%lld
    ",minn);
    }
  • 相关阅读:
    静态库与动态库的创建与使用
    MinGW 仿 linux 开发环境
    SICP 1.7-1.8 solution (Scheme)
    PHP 学生管理系统实现
    【2014最新】常用hosts集锦,分享给大家
    【Android快速入门3】布局简介及例子
    【Android快速入门2】拨号器的实现
    【Android快速入门1】目录结构及adb命令(以API19为例)
    基于深度及广度优先搜索的迷宫问题的演示
    基于HTML5的js构造爱心,动态时间校准
  • 原文地址:https://www.cnblogs.com/hao-tian/p/10086656.html
Copyright © 2011-2022 走看看