You are given 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 from this tower.
Your task is to find minimal 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.
If 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 from this tower.
Input
The first line contains two positive integers n and m (1 ≤ n, m ≤ 105) — the number of cities and the number of cellular towers.
The second line contains a sequence of n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the coordinates of cities. It is allowed that there are any number of cities in the same point. All coordinates ai are given in non-decreasing order.
The third line contains a sequence of m integers b1, b2, ..., bm ( - 109 ≤ bj ≤ 109) — the coordinates of cellular towers. It is allowed that there are any number of towers in the same point. All coordinates bj are given in non-decreasing order.
Output
Print minimal r so that each city will be covered by cellular network.
Examples
input
Copy
3 2 -2 2 4 -3 0
output
Copy
4
input
Copy
5 3 1 5 10 14 17 4 11 15
output
Copy
3
题意:
有一条x轴,上面有服务点和城市,每一个服务点有相同的服务半径,若一个城市在一个服务点的半径内,它就被服务,让你找出最小半径,使得所有城市都被服务。
思路:
二分
code:
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
LL n, m, a[100100], b[100100];
void input() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
scanf_s("%lld", &a[i]);
for (int i = 1; i <= m; i++)
scanf_s("%lld", &b[i]);
for (int i = 1; i <= n; i++)
a[i] += 1e9;
for (int i = 1; i <= m; i++)
b[i] += 1e9;
sort(a + 1, a + 1 + n);
sort(b + 1, b + 1 + m);
}
bool pang(LL x) {
int t1 = 1, t2 = 1;
while (t1 <= n&&t2<=m) {
if (a[t1] <= b[t2] + x && a[t1] >= b[t2] - x)
t1++;
else
t2++;
}
if (t1 > n)
return 1;
else
return 0;
}
void solve() {
LL l = 0, r = 2e9 + 5,ans;
while (l <= r) {
LL mid = (l + r) / 2;
if (pang(mid)) {
r = mid - 1;
ans = mid;
}
else {
l = mid + 1;
}
}
cout << ans << endl;
}
int main()
{
input();
solve();
return 0;
}