AtCoder 4167 Equal Cut【套路】

https://arc100.contest.atcoder.jp/tasks/arc100_b?lang=en
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

将一个数列分成四个不为空的片段,然后使得区间和最大的片段与区间和最小的片段的差值最小化。

CF上有类似的题目,可以枚举中线在中线两侧进行二分查找左右中点。理论上是有四种情况需要讨论(因为中点的左右并没有办法保证)。为了保险,写了七种判断,后经检验四种足矣。

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+25;
#define ll long long int
#define INF 0x3f3f3f3f
int n;
int a[maxn];
ll sum[maxn];
ll b[5];
int main()
{
    int n;
    scanf("%d",&n);
    memset(sum,0,sizeof sum);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        sum[i]=sum[i-1]+a[i];
    }
    ll ans=-1;
    for(int i=2;i<=n-1;i++){
        ll s1=sum[i];
        ll s2=sum[n]-sum[i];
        ll x1,x2,y1,y2;
        ll av1=s1/2;ll av2=s2/2;
        // 1.0
        x1=lower_bound(sum+1,sum+i+1,av1)-sum;
        y1=lower_bound(sum+i+2,sum+1+n,s1+av2)-sum;
       // cout<<x1<<" "<<y1<<endl;
        b[1]=sum[x1];
        b[2]=s1-sum[x1];
        b[3]=sum[y1]-s1;
        b[4]=s2-b[3];
        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;
        sort(b+1,b+5);
        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;
        if(ans==-1){
            ans=abs(b[4]-b[1]);
        }
        ans=min(ans,abs(b[4]-b[1]));
        //cout<<ans<<endl;


        x1=lower_bound(sum+1,sum+i+1,av1)-sum;
        y1=lower_bound(sum+i+2,sum+1+n,s1+av2)-sum;
       // cout<<x1<<" "<<y1<<endl;
        x1--;y1--;
        b[1]=sum[x1];
        b[2]=s1-sum[x1];
        b[3]=sum[y1]-s1;
        b[4]=s2-b[3];
        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;
        sort(b+1,b+5);
        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;
        if(ans==-1){
            ans=abs(b[4]-b[1]);
        }
        ans=min(ans,abs(b[4]-b[1]));



        x1=lower_bound(sum+1,sum+i+1,av1)-sum;
        y1=lower_bound(sum+i+2,sum+1+n,s1+av2)-sum;
       // cout<<x1<<" "<<y1<<endl;
        x1--;
        b[1]=sum[x1];
        b[2]=s1-sum[x1];
        b[3]=sum[y1]-s1;
        b[4]=s2-b[3];
        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;
        sort(b+1,b+5);
        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;
        if(ans==-1){
            ans=abs(b[4]-b[1]);
        }
        ans=min(ans,abs(b[4]-b[1]));


        x1=lower_bound(sum+1,sum+i+1,av1)-sum;
        y1=lower_bound(sum+i+2,sum+1+n,s1+av2)-sum;
       // cout<<x1<<" "<<y1<<endl;
        y1--;
        b[1]=sum[x1];
        b[2]=s1-sum[x1];
        b[3]=sum[y1]-s1;
        b[4]=s2-b[3];
        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;
        sort(b+1,b+5);
        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;
        if(ans==-1){
            ans=abs(b[4]-b[1]);
        }
        ans=min(ans,abs(b[4]-b[1]));

        //2.0
        av1=s1/2;av2=s2/2;
        x1=upper_bound(sum+1,sum+i+1,av1)-sum;
        y1=lower_bound(sum+i+2,sum+1+n,s1+av2)-sum;
       // cout<<x1<<" "<<y1<<endl;
        b[1]=sum[x1];
        b[2]=s1-sum[x1];
        b[3]=sum[y1]-s1;
        b[4]=s2-b[3];
        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;
        sort(b+1,b+5);
        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;
        if(ans==-1){
            ans=abs(b[4]-b[1]);
        }
        ans=min(ans,abs(b[4]-b[1]));

        //3.0
        av1=s1/2;av2=s2/2;
        x1=lower_bound(sum+1,sum+i+1,av1)-sum;
        y1=upper_bound(sum+i+2,sum+1+n,s1+av2)-sum;
       // cout<<x1<<" "<<y1<<endl;
        b[1]=sum[x1];
        b[2]=s1-sum[x1];
        b[3]=sum[y1]-s1;
        b[4]=s2-b[3];
        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;
        sort(b+1,b+5);
        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;
        if(ans==-1){
            ans=abs(b[4]-b[1]);
        }
        ans=min(ans,abs(b[4]-b[1]));

        //4.0
        av1=s1/2;av2=s2/2;
        x1=upper_bound(sum+1,sum+i+1,av1)-sum;
        y1=upper_bound(sum+i+2,sum+1+n,s1+av2)-sum;
       // cout<<x1<<" "<<y1<<endl;
        b[1]=sum[x1];
        b[2]=s1-sum[x1];
        b[3]=sum[y1]-s1;
        b[4]=s2-b[3];
        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;
        sort(b+1,b+5);
        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;
        if(ans==-1){
            ans=abs(b[4]-b[1]);
        }
        ans=min(ans,abs(b[4]-b[1]));

    }
    printf("%lld\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值