题目链接: http://codeforces.com/problemset/problem/875/E
题目大意:有两个快递员A, B, 他们的初始坐标为 s1,s2 , 有n个需要送的地点坐标为 a1...n , 按照派送优先顺序编号。 求两个快递员派送过程中相隔最大距离的最小值。 (n≤105,s1,s2,ai≤109)
思路: 考虑二分答案来判断合法性。 若二分了答案为x, 倒过来考虑这n个点。 我们考虑计算当其中一个快递员在 ai 时, 另一个快递员所能允许存在的坐标区间[l, r]。 考虑刚开始时, 一个快递员在a[n], 显然所对应的区间为[ an−x,an+x ]。 考虑到 ai 时, 若 ai 在当前区间[l, r]内, 则另一个快递员可以移动到 ai , 新的区间改为[ ai−x,ai+x ]。 否则, ai 只能由我当前这个快递员去送, 则新的区间应为当前区间[l, r]与[ ai−x,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;
}