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\n",minn);
}

原文地址:https://www.cnblogs.com/hao-tian/p/10086656.html

时间: 2024-10-10 03:41:15

Equal Cut的相关文章

ARC100 D - Equal Cut

D - Equal Cut Time limit : 2sec / Memory limit : 1024MB Score : 600 points Problem Statement 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 positi

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

原文链接https://www.cnblogs.com/zhouzhendong/p/9251420.html 题目传送门 - ARC100D 题意 给你一个长度为 $n$ 的数列,请切 $3$ 刀,形成 $4$ 个连续非空子序列,问这 $4$ 个非空子序列的各自的元素和 的极差为多少. $n\leq 2\times 10 ^5$ 题解 如果切一刀,那么问题就很简单,尽量选中间的就可以了. 可以二分一下在 $O(\log n)$ 的复杂度内解决. 于是本题可以先枚举一下中间那条线,然后对于两边转

HDU 3305 Ice-sugar Gourd

Ice-sugar Gourd Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 936    Accepted Submission(s): 329 Problem Description Ice-sugar gourd, “bing tang hu lu”, is a popular snack in Beijing of China.

ARC 100

链接 https://arc100.contest.atcoder.jp/ C Linear Approximation 题解 把ai减去i后排序, 我们要的b就是排完序后的中位数 Code 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 5 ll read(){ 6 ll x=0,f=1;char c=getchar(); 7 while(c<'0' || c>'9'){if(c

AtCoder Beginner Contest 102

A - Multiple of 2 and N Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement You are given a positive integer NN. Find the minimum positive integer divisible by both 22 and NN. Constraints 1≤N≤1091≤N≤109 All values in inp

【AtCoder】ARC100 题解

C - Linear Approximation 找出\(A_i - i\)的中位数作为\(b\)即可 题解 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #define enter putchar('\n') #define space putchar(' ') #define fi first #define se second #define

Ice-sugar Gourd

3083: Ice-sugar Gourd 描述 Ice-sugar gourd, “bing tang hu lu”, is a popular snack in Beijing of China. It is made of some fruits threaded by a stick. The complicated feeling will be like a both sour and sweet ice when you taste it. You are making your

linux常用命令-文本处理cut,sort,uniq,wc,tr

cut:截取文本特定字段 NAME       cut - remove sections from each line of files -d, --delimiter=DELIM(指定字段分隔符,默认是空格) use DELIM instead of TAB for field delimiter -f, --fields=LIST(指定要显示的字段) select  only  these  fields;  also print any line that contains no del

Lintcode: Wood Cut

Given n pieces of wood with length L[i] (integer array). Cut them into small pieces to guarantee you could have equal or more than k pieces with the same length. What is the longest length you can get from the n pieces of wood? Given L & k, return th