【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 二分+线段树的更多相关文章

  1. HDU4614 Vases and Flowers 二分+线段树

    分析:感觉一看就是二分+线段树,没啥好想的,唯一注意,当开始摆花时,注意和最多能放的比大小 #include<iostream> #include<cmath> #includ ...

  2. J - Joseph and Tests Gym - 102020J (二分+线段树)

    题目链接:https://cn.vjudge.net/contest/283920#problem/J 题目大意:首先给你n个门的高度,然后q次询问,每一次询问包括两种操作,第一种操作是将当前的门的高 ...

  3. Educational Codeforces Round 61 D 二分 + 线段树

    https://codeforces.com/contest/1132/problem/D 二分 + 线段树(弃用结构体型线段树) 题意 有n台电脑,只有一个充电器,每台电脑一开始有a[i]电量,每秒 ...

  4. 【BZOJ-3110】K大数查询 整体二分 + 线段树

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 6265  Solved: 2060[Submit][Sta ...

  5. hdu6070 Dirt Ratio 二分+线段树

    /** 题目:hdu6070 Dirt Ratio 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意:给定n个数,求1.0*x/y最小是多少.x ...

  6. K-th occurrence HDU - 6704 (后缀数组+二分线段树+主席树)

    大意: 给定串s, q个询问(l,r,k), 求子串s[l,r]的第kk次出现位置. 这是一篇很好的题解: https://blog.csdn.net/sdauguanweihong/article/ ...

  7. HDU5008 Boring String Problem(后缀数组 + 二分 + 线段树)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5008 Description In this problem, you are given ...

  8. hdu 5649 DZY Loves Sorting 二分+线段树

    题目链接 给一个序列, 两种操作, 一种是将[l, r]里所有数升序排列, 一种是降序排列. 所有操作完了之后, 问你a[k]等于多少. 真心是涨见识了这题..好厉害. 因为最后只询问一个位置, 所以 ...

  9. (困难) CF 484E Sign on Fence,整体二分+线段树

    Bizon the Champion has recently finished painting his wood fence. The fence consists of a sequence o ...

随机推荐

  1. what's the help of "unnecessary" pointer comparison

    引述自http://c-programming.itags.org/q_c-programming-language_191518.html 源代码中的宏min中使用了 (void) (&_x ...

  2. css只改变input输入框光标颜色不改变文字颜色实现方法

    input:focus{color:blue}//光标颜色 input{ text-shadow: 0px 0px 0px red;//文字颜色 -webkit-text-fill-color: tr ...

  3. geoserver REST使用

    1.部署一个简单的测试环境 测试geoserver REST接口,我们可使用python来测试,很方便.需要下载包: python,http://python.org/.我下载的是Python27版本 ...

  4. Uva--11324--The Largest Clique【有向图强连通分量】

    链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&am ...

  5. 让树莓派自动上报IP地址到邮箱,二代B

    由于我使用树莓派的场景大多数是在没有显示器.只用terminal连接它的情况下,所以,它的IP地址有时会在重启之后变掉(DHCP的),导致我无法通过terminal连接上它.然后我又要很麻烦地登录路由 ...

  6. vuejs时间格式化

    date.js export function formatDate(date, fmt) { if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, ...

  7. 安装eclipse,配置tomcat

    1.去官网(https://www.eclipse.org/downloads/download.php?file=/oomph/epp/oxygen/R/eclipse-inst-win64.exe ...

  8. C#调用DLL报“试图加载格式不正确的程序”

    项目右键属性->项目设计器->生成->平台->把'默认设置(任何 CPU)'改为x86或者x64

  9. 在wepy里面使用redux

    wepy 框架本身是支持 Redux 的,我们在构建项目的时候,将 是否安装 Redux 选择 y 就好了,会自动安装依赖,运行项目后看官方给的 demo 确实是可以做到的,但是官方文档里却对这一块只 ...

  10. Hibernate系列之ID生成策略

    一.概述 hibernate中使用两种方式实现主键生成策略,分别是XML生成id和注解方式(@GeneratedValue),下面逐一进行总结. 二.XML配置方法 这种方式是在XX.hbm.xml文 ...