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 ...
随机推荐
- DevOps专题|基础Agent部署系统
随着京东云业务规模.管理机器规模的扩大,各类agent也在逐渐增多,如日志agent.监控agent.控制系统agent等.这对agent的部署.升级.状态维护提出了很高的要求,一旦某个全局agent ...
- 关于Pytorch中autograd和backward的一些笔记
参考自<Pytorch autograd,backward详解>: 1 Tensor Pytorch中所有的计算其实都可以回归到Tensor上,所以有必要重新认识一下Tensor. 如果我 ...
- tf.summary可视化参数
1.tf.summary.scalar('accuracy', accuracy) 损失值.准确率随着迭代次数的进行,其指标变化情况:一般在画loss,accuary时会用到这个函数. 2.tenso ...
- iOS基础——通过案例学知识之xib、plist、mvc
透过案例学习xib的使用.plist的使用.mvc在iOS的使用,今天要做的案例效果图 1.xib和nib xib文件可以被XCode编译成nib文件,xib文件本质上是一个xml文件,而nib文件就 ...
- Day 9:双列集合Map及实现该接口的类的常用方法
为什么要学双列集合? 因为单列集合无法处理映射关系,会有成对出现的数据 Map接口 如果是实现了Map接口的集合类,具备的特点: 存储的数据都是以键值对的形式存在的,键不可重复,值可以重复 Map接 ...
- 关于mysql/apache/nginx的配置文件配置
2015.5.12 mysql中可以通过函数查看用户列表/查看当前用户以及删除用户,方法百度之,忘了 /etc/php5/fpm/php.ini memory_limit修改之前为128M apach ...
- python一个正则表达式的不解
htmlSource="data-lazy=\"http://gtms01.alicdn.com/tps/i1/T1faOCFQXXXXc2jIrl-.png\"&quo ...
- MySQL高负载优化
MySQL配置文件优化 [client] port = #客户端端口号为3306 socket = /data//mysql.sock # default-character-set = utf8 # ...
- nodejs(11)Express 中进行数据库操作
配置 MySql 数据库环境 mysql 第三方模块的介绍和基本配置 要安装操作数据库的第三方包npm i mysql -S 导入 包 const mysql = require('mysql') 创 ...
- mysql分组和排序操作
分组.排序操作 sele ...