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 ...
随机推荐
- 1, vm: PropTypes.instanceOf(VM).isRequired
子模块的文件引入父工程对象时,出现红色warning,提示传入的对象类型不是所要求的类型. 思路是父工程引用的JS包和子模块使用的包不是同一个包,解决办法是父工程和子工程都使用同一个包. resolv ...
- Palette 的使用
Palette有什么用? Palette主要功能就是可以从图片中提取各种与颜色有关的元素.通过使用 Palette ,我们可以很轻松的实现界面风格的统一. Palette的使用很简单,首先你可以从gi ...
- ES系列之Promise async 和 await
概述 promise是异步编程的一种解决方案,比传统的解决方案—回调函数和事件—更合理更强大. 所谓的promise就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作的结果). Pro ...
- .NET技术-2.0. 操作数据库-Dapper
.NET技术-2.0. 操作数据库-Dapper 项目参见: 1. 为什么选择Dapper 1) 性能优越: 其实在各大网站上,我们大概都会看到这样的一个对比效果图,在超过500次poco seria ...
- JavaScript 2019.3.15
方法名.call(对象)可以切换方法调用的对象 参数数量 基本数据类型 typeof无法更细致的区分引用类型(全是object) =
- python对数组缺失值进行填充
1. 两个常用的函数 1.1 np.nonzero() np.nonzero()函数返回数组中不为False(0)的元素对应的索引 a = np.array([1,2,0,3,1,0]) print( ...
- Thread--生产者消费者
2个生产者,2个消费者,库存容量2 package p_c_allWait.copy; import java.util.LinkedList; import java.util.List; publ ...
- git常用操作及其基本命令
克隆远程仓库代码到本地 本地创建有文件夹 git clone 远程仓库地址 本地文件夹名称 本地没有创建文件夹 git clone 远程仓库地址 文件夹名称 克隆完成之后,使用“cd 文件夹”的方式进 ...
- F5负载均衡综合实例详解(转)
转载自:https://blog.csdn.net/weixin_43089453/article/details/87937994 女程序员就不脱发了吗来源于:<网络运维与管理>201 ...
- aws ec2 安装Elastic search 7.2.0 kibana 并配置 hanlp 分词插件
文章大纲 Elastic search & kibana & 分词器 安装 版本控制 下载地址 Elastic search安装 kibana 安装 分词器配置 Elastic sea ...