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 ...
随机推荐
- (排序)P1177 【模板】快速排序
题解: 这道题用传统快排(如下所示)的结果就是最后三个点TLE: 如果永远取第一个元素作为枢轴的话,在数组已经有序的情况下每次划分都将得到最坏的结果,时间复杂度退化为O(n^2).因为其中一个子序列每 ...
- 寒假day25
今天解决了部分数据爬取不下来的问题,同时进行了面试的准备
- BZOJ 3197 [Sdoi2013]assassin
题解: 树上Hash 首先重心在边上就把边分裂 以重心为根建树,这样两个根一定对应 然后f[i][j]表示i匹配另一棵的j节点的最小代价 把他们的儿子摘出来做最小权匹配即可 #include<i ...
- 关于域名转发proxy_pass
在配置nginx的时候,有一个需求,访问m.XXX.com的时候,需要实际访问www.YYY.com/m,并且域名不能发生变化. 达成这个需求有两种做法: 第一种就是301跳转,使用rewrite来跳 ...
- 改变UILable里面文字的大小和颜色
UILabel *lb = [[UILabel alloc]init]; NSMutableAttributedString *attriStr = [[NSMutableAttributedStri ...
- Odd sum (对本菜鸡来说是个极坑题)
https://codeforces.com/problemset/problem/797/B time limit per test 1 second memory limit per test 2 ...
- linux.linuxidc.com - /2011年资料/Android入门教程/
本文转自 http://itindex.net/detail/15843-linux.linuxidc.com-%E8%B5%84%E6%96%99-android Shared by Yuan 用户 ...
- sudo apt-get update数字签名错误解决方法
lzb@lzb:~/projects/curl-master$ sudo apt-get update 命中: http://mirrors.aliyun.com/ubuntu xenial InRe ...
- mybatis 在自动生成时设置不生成Example类
只需要在配置要生成的table表中添加几个配置属性就行了. 在generatorConfig.xml文件中修改 <!--指定数据库表--> <table tableName=&quo ...
- 【网易官方】极客战记(codecombat)攻略-森林-流星雨star-shower
流星雨不仅是一个了不起的现象,而且是获得一些钱的好机会. 简介 流星雨正在下着你的宝石和硬币! 但星形金属不是很长寿,硬币很快就消失了. 宝石不会消失. 使用或语句提取密切的金币或宝石: if ite ...