coderforces 702C Cellular Network(二分)

来源:互联网 发布:消防大数据平台 编辑:程序博客网 时间:2024/05/22 10:30
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 31 5 10 14 174 11 15
output

3

题意:给你n个城市和m和信号塔的坐标位置,信号塔的覆盖半径为r,问你能把所有城市都覆盖的r的最小值

思路:枚举每个城市,用二分查找距离这个城市最近的信号塔,并记录它们之间的距离,最后找到城市与信号塔之间的距离中的最大值即为所求,具体见代码。。

#include <cstdio>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;const int maxn = 100010;int n,m;int a[maxn];int b[maxn];int main(){    while(~scanf("%d%d",&n,&m))    {        for(int i = 0 ; i < n ; i++)            scanf("%d",&a[i]);        for(int j = 0 ; j < m ; j++)            scanf("%d",&b[j]);        int ans = 0;        for(int i = 0 ; i < n ; i++)        {            int l = 0 , r = m-1;            while(r - l > 1)            {                int mid = (l + r) >> 1;                if(b[mid] >= a[i])                    r = mid;                else                    l = mid;            }            int cnt = min(abs(b[l]-a[i]),abs(b[r]-a[i]));            ans = max(ans,cnt);        }        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击