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 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

Input is given from Standard Input in the following format:

N A1 A2  AN 

Output

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


Sample Input 1

Copy

5 3 2 4 1 2 

Sample Output 1

Copy

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.


Sample Input 2

Copy

10 10 71 84 33 6 47 23 25 52 64 

Sample Output 2

Copy

36 

Sample Input 3

Copy

7 1 2 3 1000000000 4 5 6 

Sample Output 3

Copy

999999994 

题解:

先做个前缀和,以快速求解区间和

考虑先切中间的那一刀,将整个序列分成左右两块,记为L,R,再将L,R分别切成2块,记为L1,L2,R1,R2

那么,|L1-L2|、|R1-R2|最小时,这样的划分一定是最优的(令L1=L/2+k,L2=L/2-k,R1=R/2+t,R2=R/2-t,显然最大值一定为max{L1,R1},最小值一定为{L2,R2},因此k,t增大时,不会使答案更优)

设中间那刀的位置为i,左边的位置为Li,右边的为Ri,注意到i增大时,Li,Ri均单调增大

考虑从小到大枚举i,同时调整Li和Ri,利用Li和Ri的单调性,即可做到O(n)

#include<bits/stdc++.h>
using namespace std;
int n;
long long ans=1e18;
int arr[200005];
long long pre[200005];
long long maxn(long long a,long long b)
{
    return a>b?a:b;
}
long long minx(long long a,long long b)
{
    return a<b?a:b;
}
int main()
{
    scanf("%d",&n);
    pre[0]=0;
    for(int i=1;i<=n;i++)
        {
            scanf("%d",&arr[i]);
            pre[i]=pre[i-1]+(long long)arr[i];
        }
    int L=0,R=2;
    for(int i=2;i<=n-2;i++)
        {
            while(pre[L]<pre[i]-pre[L])
                  L++;
            if(2*pre[L]-pre[i]>pre[i]-2*pre[L-1])
               L--;
            while(pre[R]-pre[i]<pre[n]-pre[R])
                  R++;
            if(2*pre[R]-pre[i]-pre[n]>pre[n]+pre[i]-2*pre[R-1])
               R--;
            long long s1=maxn(pre[L],pre[i]-pre[L]);
            long long s2=maxn(pre[n]-pre[R],pre[R]-pre[i]);
            long long s3=minx(pre[L],pre[i]-pre[L]);
            long long s4=minx(pre[n]-pre[R],pre[R]-pre[i]);
            long long mx=maxn(s1,s2);
            long long mn=minx(s3,s4);
            ans=minx(ans,mx-mn);
        }
    printf("%lld",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/nanjolno/p/9349758.html

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

ARC100 D - Equal Cut的相关文章

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)$ 的复杂度内解决. 于是本题可以先枚举一下中间那条线,然后对于两边转

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, res

【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

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

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.

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

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