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.
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.
Print minimal r so that each city will be covered by cellular network.
3 2 -2 2 4 -3 0
4
5 3 1 5 10 14 17 4 11 15
3
题目大意:
一共有N个城市和M个蜂巢,每个蜂巢为其周边半径小于R的城市服务,城市和蜂巢排列成一行(坐标轴上);
问这个R最小是多少,使得所有城市都被服务。
思路:
1、明显R==5如果可以的话,那么R==6.7.8.........都是可以的,那么这里包含一个单调性,我们可以二分这个最小R.
2、对于当前二分出来的mid值,我们暴力O(n)处理是否当前这个mid值是可行的,如果可行,减小R,否则增大R即可。
过程记录最后一次可行解,进行输出。
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64
ll a[100050];
ll b[100050];
int n,m;
int Slove(ll mid)
{
int now=0;
for(int i=0;i<m;i++)
{
ll left=b[i]-mid;
ll right=b[i]+mid;
while(now<n)
{
if(a[now]>=left&&a[now]<=right)now++;
else break;
}
}
if(now==n)return 1;
else return 0;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<n;i++)scanf("%I64d",&a[i]);
for(int i=0;i<m;i++)scanf("%I64d",&b[i]);
ll ans=0;
ll l=0;
ll r=2000000000000;
while(r-l>=0)
{
ll mid=(l+r)/2;
if(Slove(mid)==1)
{
ans=mid;
r=mid-1;
}
else l=mid+1;
}
printf("%I64d\n",ans);
}
}