【CF875E】Delivery Club 二分+线段树
【CF875E】Delivery Club
题意:有n个快递需要依次接收,这n个快递分部在x轴上,第i个快递的位置是xi。有两个快递员,一开始分别在s0,s1,你可以任意安排哪个人收哪个快递,前提是一个快递员收快递是另一个快递员不能移动(也就是说他只有在收快递时能移动),并且要保证任何时候两人的距离不超过k。问你k最小是多少。
n<=10^5,xi<=10^9
题解:二分是显然的。我们可以用f[i][a][b]表示收第i个快递时,两个快递员一个在a,一个在b是否可行,又因为a或b一定等于i,所以我们可以省掉一维。我们还可以用线段树再省一维。因为在收第i+1个快递时,要么是在i处的快递员走到i+1,此时与i+1距离超过k的位置a都变成了不合法的,可以用线段树区间清零搞定;要么是在a处的快递员走到i+1。用线段树很容易维护这些东西。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lson x<<1
#define rson x<<1|1
using namespace std;
const int maxn=100010;
int n;
int v[maxn],p[maxn],rnk[maxn];
bool s[maxn<<2];
inline int Abs(const int &a) {return a>0?a:-a;}
bool cmp(const int &a,const int &b)
{
return v[a]<v[b];
}
inline void pushdown(int x)
{
if(!s[x]) s[lson]=s[rson]=0;
}
void modify(int l,int r,int x,int a)
{
if(l==r)
{
s[x]=1;
return ;
}
pushdown(x);
int mid=(l+r)>>1;
if(a<=mid) modify(l,mid,lson,a);
else modify(mid+1,r,rson,a);
s[x]=s[lson]|s[rson];
}
void updata(int l,int r,int x,int a,int b)
{
if(a>b) return ;
if(a<=l&&r<=b)
{
s[x]=0;
return ;
}
pushdown(x);
int mid=(l+r)>>1;
if(a<=mid) updata(l,mid,lson,a,b);
if(b>mid) updata(mid+1,r,rson,a,b);
s[x]=s[lson]|s[rson];
}
bool check(int len)
{
int i,l,r,mid;
s[1]=0;
modify(1,n,1,rnk[1]);
for(i=3;i<=n;i++)
{
l=1,r=rnk[i];
while(l<r)
{
mid=(l+r)>>1;
if(v[p[mid]]<v[i]-len) l=mid+1;
else r=mid;
}
updata(1,n,1,1,l-1);
l=rnk[i],r=n;
while(l<r)
{
mid=(l+r)>>1;
if(v[p[mid]]<=v[i]+len) l=mid+1;
else r=mid;
}
updata(1,n,1,l,n);
if(Abs(v[i]-v[i-1])<=len) modify(1,n,1,rnk[i-1]);
if(!s[1]) return 0;
}
return 1;
}
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-') f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+(gc^'0'),gc=getchar();
return ret*f;
}
int main()
{
n=rd()+2;
int i,l=0,r=0,mid;
for(i=1;i<=n;i++) v[i]=rd(),r=max(r,v[i]),p[i]=i;
sort(p+1,p+n+1,cmp);
for(i=1;i<=n;i++) rnk[p[i]]=i;
v[0]=-1000000000,v[n+1]=1000000000;
l=Abs(v[2]-v[1]);
while(l<r)
{
mid=(l+r)>>1;
if(check(mid)) r=mid;
else l=mid+1;
}
printf("%d",r);
return 0;
}
【CF875E】Delivery Club 二分+线段树的更多相关文章
- HDU4614 Vases and Flowers 二分+线段树
分析:感觉一看就是二分+线段树,没啥好想的,唯一注意,当开始摆花时,注意和最多能放的比大小 #include<iostream> #include<cmath> #includ ...
- J - Joseph and Tests Gym - 102020J (二分+线段树)
题目链接:https://cn.vjudge.net/contest/283920#problem/J 题目大意:首先给你n个门的高度,然后q次询问,每一次询问包括两种操作,第一种操作是将当前的门的高 ...
- Educational Codeforces Round 61 D 二分 + 线段树
https://codeforces.com/contest/1132/problem/D 二分 + 线段树(弃用结构体型线段树) 题意 有n台电脑,只有一个充电器,每台电脑一开始有a[i]电量,每秒 ...
- 【BZOJ-3110】K大数查询 整体二分 + 线段树
3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 6265 Solved: 2060[Submit][Sta ...
- hdu6070 Dirt Ratio 二分+线段树
/** 题目:hdu6070 Dirt Ratio 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意:给定n个数,求1.0*x/y最小是多少.x ...
- K-th occurrence HDU - 6704 (后缀数组+二分线段树+主席树)
大意: 给定串s, q个询问(l,r,k), 求子串s[l,r]的第kk次出现位置. 这是一篇很好的题解: https://blog.csdn.net/sdauguanweihong/article/ ...
- HDU5008 Boring String Problem(后缀数组 + 二分 + 线段树)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5008 Description In this problem, you are given ...
- hdu 5649 DZY Loves Sorting 二分+线段树
题目链接 给一个序列, 两种操作, 一种是将[l, r]里所有数升序排列, 一种是降序排列. 所有操作完了之后, 问你a[k]等于多少. 真心是涨见识了这题..好厉害. 因为最后只询问一个位置, 所以 ...
- (困难) CF 484E Sign on Fence,整体二分+线段树
Bizon the Champion has recently finished painting his wood fence. The fence consists of a sequence o ...
随机推荐
- [转]python的requests发送/上传多个文件
1.需要的环境 Python2.X Requests 库 2.单字段发送单个文件 在requests中发送文件的接口只有一种,那就是使用requests.post的files参数, 请求形式如下: ...
- jquery与php的HTML转义与反转义
1.jquery (1)Html转义 var tmp = '<a href="https://www.baidu.com/">连接</a>'; var tm ...
- 使用java启动mybatis generator
1.java工程目录结构: |src/main/java |com.leslie.mybatis.generator |MybatisGeneratorUtil.java |src/main/reso ...
- rpc框架 SimpleXMLRP
一台电脑去控制另一台电脑干事情,最常见的就是web. 用户get/post一个url,服务端根据url地址和参数去做相应的事情把结果返回给客户端. server.py #coding=utf8 fro ...
- Objective-C 协议和运行时检查方法、类是否存在
协议的声明: // // Person.h // TestOC01 // // Created by xinye on 13-10-23. // Copyright (c) 2013年 xinye. ...
- VSCode------.net core2.0发布后配置到Window Service 2008R2报500错误
如图: 解决方法: 出现这个错误是因为 IIS 采用了更安全的 web.config 管理机制,默认情况下会锁住配置项不允许更改. 要取消锁定可以运行命令行 %windir%\system32\ine ...
- GoF--适配器设计模式
1.概念: 适配器模式(Adapter Pattern)把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 2.形式 a.类的适配器模式 ...
- Git Step by Step – (6) Git远程仓库
前面文章中出现的所有Git操作都是基于本地仓库的,但是日常工作中需要多人合作,不可能一直都在自己的代码仓库工作.所以,这里我们就开始介绍Git远程仓库. 在Git系统中,用户可以通过push/pull ...
- iOS - AVAudioSession详解
音频输出作为硬件资源,对于iOS系统来说是唯一的,那么要如何协调和各个App之间对这个稀缺的硬件持有关系呢? iOS给出的解决方案是"AVAudioSession" ,通过它可以实 ...
- Android学习之Handler消息
Android系统规定,一些耗时的操作不能放在UI线程中去执行,这样会报一个ANR错误.所以为了避免该问题,我们需要开启一个新的线程去执行一些耗时操作:开启新的线程,将耗时的操作在新线程里面去执行, ...