[Codeforces 875E] Delivery Club

题目链接: http://codeforces.com/problemset/problem/875/E

题目大意:有两个快递员A, B, 他们的初始坐标为 s1,s2 , 有n个需要送的地点坐标为 a1...n , 按照派送优先顺序编号。 求两个快递员派送过程中相隔最大距离的最小值。 (n105,s1,s2,ai109)

思路: 考虑二分答案来判断合法性。 若二分了答案为x, 倒过来考虑这n个点。 我们考虑计算当其中一个快递员在 ai 时, 另一个快递员所能允许存在的坐标区间[l, r]。 考虑刚开始时, 一个快递员在a[n], 显然所对应的区间为[ anx,an+x ]。 考虑到 ai 时, 若 ai 在当前区间[l, r]内, 则另一个快递员可以移动到 ai , 新的区间改为[ aix,ai+x ]。 否则, ai 只能由我当前这个快递员去送, 则新的区间应为当前区间[l, r]与[ aix,ai+x ]取交集, 若无交集直接返回非法。 最后只要判断 s1 s2 是否在最后的区间里即可。

#include <cstdio>
#include <cstdlib>
#include <algorithm>

using namespace std;

const int N = (int)1e5 + 10;

int n, s1, s2, a[N]; 

bool check(int x){
    int l = a[n] - x, r = a[n] + x;
    for (int i = n - 1; i >= 1; i --){
        if (l <= a[i] && a[i] <= r)
            l = a[i] - x, r = a[i] + x;
        else{
            l = max(a[i] - x, l);
            r = min(a[i] + x, r);
            if (l > r) return 0;
        }
    }

    return (l <= s1 && s1 <= r) || (l <= s2 && s2 <= r);
}

int main(){
    scanf("%d%d%d", &n, &s1, &s2);
    if (s1 < s2) swap(s1, s2);
    for (int i = 1; i <= n; i ++) scanf("%d", a + i);

    int l = s1 - s2, r = (int)1e9, ans;
    while (l <= r){
        int mid = (l + r) >> 1;
        if (check(mid)) ans = mid, r = mid - 1;
        else l = mid + 1;
    }

    printf("%d\n", ans);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值