codeforces 702C Cellular Network 2016-10-15 18:19 104人阅读 评论(0) 收藏

时间:2022-05-24 01:01:39
C. Cellular Network
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

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
3 2
-2 2 4
-3 0
output
4
input
5 3
1 5 10 14 17
4 11 15
output
3
题目的意思是在一条直线上,给出两类点,一种是城市,一种是信号台,问信号台的覆盖范围最小为多少才能覆盖所有城市。
实际上就是找出每个城市理他最近的信号台的距离去最大值即可。
我们先将城市和信号台排个序,然后定义两个下标p,q进行访问城市和信号台,因为在排序的情况下,如果后一个信号台比前一个距离某城市更近,那么它一定比前一个距离下一个城市更近,我们用dp数组记录下所有最近距离,在去最大即可





#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f long long int a[100005];
long long int b[100005];
long long int dp[100005];
int m,n;
int main()
{
while(~scanf("%d%d",&m,&n))
{
for(int i=0; i<m; i++)
{
scanf("%I64d",&a[i]);
} for(int j=0; j<n; j++)
{
scanf("%I64d",&b[j]);
} sort(a,a+m);
sort(b,b+n);
memset(dp,inf,sizeof dp);
int q=0,p=0;
while(q<m&&p<n)
{
if(abs(a[q]-b[p])<=dp[q])
{
dp[q]=abs(a[q]-b[p]);
p++;
}
else
{
p--;
q++;
}
}
while(q<m)
{
dp[q]=abs(a[q]-b[n-1]);
q++;
}
long long sum=-1;
for(int i=0; i<m; i++)
sum=max(sum,dp[i]);
printf("%I64d\n",sum);
}
return 0;
}