3 seconds
256 megabytes
standard input
standard output
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
题意:
让你给每一个城市找一个塔来覆盖,然后叫你求出最小的覆盖半径,城市和塔都在一条直线上
题解:
我们直接枚举每一个城市,然后二分找一个最近的塔来覆盖
#include<bits/stdc++.h>
#define F(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
typedef long long ll; const int N=1E5+;
int a[N],b[N]; void up(int &a,int b){if(a<b)a=b;} int main(){
int n,m,eda=,edb=;
scanf("%d%d",&n,&m);
F(i,,n)scanf("%d",a+i);
F(i,,m)scanf("%d",b+i);
F(i,,n)if(a[i]!=a[eda])a[++eda]=a[i];//去重
F(i,,m)if(b[i]!=b[edb])b[++edb]=b[i];//去重
int ans=;
F(i,,eda){
int pos=lower_bound(b+,b++edb,a[i])-b-;
int tp=INT_MAX;//找一个最近的塔来更新答案
if(pos>=)tp=min(tp,a[i]-b[pos]);
if(pos<edb)tp=min(tp,b[pos+]-a[i]);
up(ans,tp);
}
printf("%d\n",ans);
return ;
}