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\n",ans);
return 0;
}

Equal Cut的更多相关文章

  1. AtCoder Regular Contest 100 Equal Cut

    Equal Cut 思路: 枚举中间那个分界点,然后两边找使得切割后差值最小的点,这个可以用双指针 代码: #include<bits/stdc++.h> using namespace ...

  2. ATcoderARC100D Equal Cut

    ARC100 D - Equal Cut Description: 给出长度为n的序列A,把这个序列分成连续的四段,最小化极差. \(4≤n≤2×10^5,4≤n≤2×10^5\) Solution: ...

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

    原文链接https://www.cnblogs.com/zhouzhendong/p/9251420.html 题目传送门 - ARC100D 题意 给你一个长度为 $n$ 的数列,请切 $3$ 刀, ...

  4. ARC-100 D - Equal Cut

    题面在这里! 我们枚举一下第2和第3段的分界点,显然这种情况下 第1与第2  和  第3与第4  之间的分界点都只有两种情况可能最优,吧这四种情况讨论一下就好了. 两边的分界点可以单调扫过去... # ...

  5. ARC100D Equal Cut

    传送门 分析 首先我们想到的肯定是n^3暴力枚举,但这显然不行.然后我们想到的就是二分了,但这题没有什么单调性,所以二分也不行.这时候我就想到了先枚举找出p2的位置再在它的左右两边找到p1和p3,但是 ...

  6. Hdu 3363 Ice-sugar Gourd(对称,圆)

    Ice-sugar Gourd Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  7. 【AtCoder】ARC100 题解

    C - Linear Approximation 找出\(A_i - i\)的中位数作为\(b\)即可 题解 #include <iostream> #include <cstrin ...

  8. Hdu3363 Ice-sugar Gourd 2017-01-16 11:39 43人阅读 评论(0) 收藏

    Ice-sugar Gourd Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. AtCoder Beginner Contest 102

    A - Multiple of 2 and N Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Stat ...

随机推荐

  1. Axure下载,附注册码

    下载地址:https://www.axure.com.cn/3510/ 汉化教程:https://www.axure.com.cn/2616/ 注册码: Axure RP 8.1.0.3377--可用 ...

  2. Centos7.4系统 httpd模式搭建文件服务器

    环境:服务环境:centos7.4 说明:搭建Apache文件服务器,下载路径为/opt/ymyg(下载路径根据实际需要自己定义) 步骤: 1.安装httpd服务   [root@localhost ...

  3. axios基础介绍

    axios基础介绍 get请求要在params中定义,post要在data中定义.

  4. POJ - 3660 Cow Contest(flod)

    题意:有N头牛,M个关系,每个关系A B表示编号为A的牛比编号为B的牛强,问若想将N头牛按能力排名,有多少头牛的名次是确定的. 分析: 1.a[u][v]=1表示牛u比牛v强,flod扫一遍,可以将所 ...

  5. 刷题46. Permutations

    一.题目说明 题目是46. Permutations,给一组各不相同的数,求其所有的排列组合.难度是Medium 二.我的解答 这个题目,前面遇到过类似的.回溯法(树的深度优先算法),或者根据如下求解 ...

  6. CPU压力测试--限制到指定范围

    作用:增加CPU使用率到指定范围 1.书写shell脚本增加CPU压力 #! /bin/bash # filename cputest.sh endless_loop() { echo -ne &qu ...

  7. one_day_one_linuxCmd---光标快捷操作

    <坚持每天学习一个 linux 命令,今天我们来学习 切换光标的常用命令> 摘要:最近经常使用 xshell 软件来远程连接各种机器,在 bin/bash 下输入各种命令,因为都是一些非常 ...

  8. CSS3 box-shadow 效果大全(内阴影,外阴影,三边阴影,双边阴影,单边阴影,细线描边…)

    /* offset-x | offset-y | color */ box-shadow: 60px -16px teal; /* offset-x | offset-y | blur-radius ...

  9. Java学习十七

    学习内容: 1.Java字符串类 1.在utf-8编码下,每个汉字占三个字节 2.字符串和byte数组之间的相互转换 将字符串转换为byte数组 byte[] arrs = str.getBytes( ...

  10. PHP核心配置详解

    基本配置-语法 1:大小写敏感 directive = value 2:运算符 | & ~ ! 3:空值的表达方式 foo = ; foo = none; foo = "none&q ...