原题链接
C. Cellular Network
time limit per test
memory limit per test
input
output
n points on the straight line — the positions (x-coordinates) of the cities and m points on the same line — the positions (x-coordinates) of the cellular towers. All towers work in the same way — they provide cellular network for all cities, which are located at the distance which is no more than r
r that each city has been provided by cellular network, i.e. for each city there is at least one cellular tower at the distance which is no more than r.
r = 0 then a tower provides cellular network only for the point where it is located. One tower can provide cellular network for any number of cities, but all these cities must be at the distance which is no more than r
Input
n and m (1 ≤ n, m ≤ 105) — the number of cities and the number of cellular towers.
n integers a1, a2, ..., an (9 ≤ ai ≤ 109) — the coordinates of cities. It is allowed that there are any number of cities in the same point. All coordinates ai
m integers b1, b2, ..., bm (9 ≤ bj ≤ 109) — the coordinates of cellular towers. It is allowed that there are any number of towers in the same point. All coordinates bj
Output
r
Examples
input
3 2 -2 2 4 -3 0
output
4
input
5 3 1 5 10 14 17 4 11 15
output
3
对于每个城市用二分找到距离它最近的tower,更新距离.
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#define maxn 100005
using namespace std;
typedef long long ll;
int num1[maxn], num2[maxn], n, m;
ll solve(){
ll maxs = 0;
for(int i = 0; i < n; i++){
int d = lower_bound(num2, num2+m, num1[i]) - num2;
ll h;
if(d == 0)
h = num2[0] - num1[i];
else if(d == m)
h = num1[i] - num2[m-1];
else{
h = min(num2[d] - num1[i], num1[i] - num2[d-1]);
}
maxs = max(maxs, h);
}
return maxs;
}
int main(){
// freopen("in.txt", "r", stdin);
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++)
scanf("%d", num1+i);
for(int j = 0; j < m; j++)
scanf("%d", num2+j);
printf("%I64d\n", solve());
return 0;
}