[Codeforces 875E] Delivery Club

来源:互联网 发布:java做手机游戏 编辑:程序博客网 时间:2024/06/05 02:08

题目链接: 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]取交集, 若无交集直接返回非法。 最后只要判断s1s2是否在最后的区间里即可。

#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;}