Equal Cut
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的更多相关文章
- AtCoder Regular Contest 100 Equal Cut
Equal Cut 思路: 枚举中间那个分界点,然后两边找使得切割后差值最小的点,这个可以用双指针 代码: #include<bits/stdc++.h> using namespace ...
- ATcoderARC100D Equal Cut
ARC100 D - Equal Cut Description: 给出长度为n的序列A,把这个序列分成连续的四段,最小化极差. \(4≤n≤2×10^5,4≤n≤2×10^5\) Solution: ...
- AtCoder Regular Contest 100 (ARC100) D - Equal Cut 二分
原文链接https://www.cnblogs.com/zhouzhendong/p/9251420.html 题目传送门 - ARC100D 题意 给你一个长度为 $n$ 的数列,请切 $3$ 刀, ...
- ARC-100 D - Equal Cut
题面在这里! 我们枚举一下第2和第3段的分界点,显然这种情况下 第1与第2 和 第3与第4 之间的分界点都只有两种情况可能最优,吧这四种情况讨论一下就好了. 两边的分界点可以单调扫过去... # ...
- ARC100D Equal Cut
传送门 分析 首先我们想到的肯定是n^3暴力枚举,但这显然不行.然后我们想到的就是二分了,但这题没有什么单调性,所以二分也不行.这时候我就想到了先枚举找出p2的位置再在它的左右两边找到p1和p3,但是 ...
- Hdu 3363 Ice-sugar Gourd(对称,圆)
Ice-sugar Gourd Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- 【AtCoder】ARC100 题解
C - Linear Approximation 找出\(A_i - i\)的中位数作为\(b\)即可 题解 #include <iostream> #include <cstrin ...
- 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) ...
- AtCoder Beginner Contest 102
A - Multiple of 2 and N Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Stat ...
随机推荐
- 在div中注入html代码
直接开始: <div id="content"class="modal-body"> </div> 在angularjs中使用如下代码, ...
- Linux下MSSQL部署
目前主要使用的red hat系列的linux版本,CentoS 7.X,MSSQL2017 微软官方说明地址:https://docs.microsoft.com/zh-cn/sql/linux/qu ...
- PAT 2018 春
A 1140 Look-and-say Sequence 简单模拟.可能要注意字符串第一个字符和最后一个字符的处理. #include <cstdio> #include <iost ...
- linux centos 7 防火墙相关
centos 7 系统 默认是开启防火墙,而且没有打开80和8080等端口. 因此,今天配置tomcat和nginx后,分别无法正常访问 访问80和8080端口都报:502错误.(错误的网关)查询资料 ...
- SQL基础教程(第2版)第4章 数据更新:4-1 数据的插入(INSERT)
第4章 数据更新:4-1 数据的插入(INSERT) ● 将列名和值用逗号隔开,分别括在()内,这种形式称为清单.● 对表中所有列进行INSERT操作时可以省略表名后的列清单.● 插入NULL时需要在 ...
- vue项目 首页开发 part3
da当拖动图标时候,只有上部分可以,下部分无响应 swiper 为根页面引用,其中的css为独立,点击swiper标签可以看见其包裹区域只有部分 那么需要修改 就需要穿透样式 外部 >> ...
- Linux-常见信号介绍
1.SIGINT 2 Ctrl + C时OS送给前台进程组中每个进程 2.SIGABRT 6 调用abort函数,进程异常终止 3 ...
- OpenMP笔记(三)
个人博客地址:http://www.bearoom.xyz/2019/02/21/openmp3/ 这一部分主要记录一些指令的使用. 一.parallel的使用 parallel是用于构造并行块的,也 ...
- CentOS7设置阿里镜像教程
阿里镜像官方地址http://mirrors.aliyun.com/ 1.点击官方提供的相应系统的帮助 :2.查看不同版本的系统操作: 为linux系统下载源设置阿里镜像的步骤 : 1. 备份 备份C ...
- MyBatis 查询结果的缓存
MyBatis的缓存指的是缓存查询结果,当以后使用相同的sql语句.传入相同的参数进行查询时,可直接从mybatis本地缓存中获取查询结果,而不必查询数据库. mybatis的缓存包括一级缓存.二级缓 ...