codeforces702C Cellular Network(二分)

本文介绍了一种通过二分查找确定最小覆盖半径r的方法,确保所有城市都能被至少一个信号塔覆盖。信号塔为距离其不超过r的城市提供网络服务。文章详细阐述了输入输出格式及示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

既然city和Tower是在一条线上求Tower的最小的d,那么可以二分来做

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
long long city[100005];
long long tow[100005];
int main(){
    ios::sync_with_stdio(false);
    int N,M;
    long long high,low,mid,temp,cnt; while(cin>>N>>M)
    {
        for(int i=0;i<N;i++)
            cin>>city[i];
        for(int i=0;i<M;i++)
            cin>>tow[i];
        sort(city,city+N);
        sort(tow,tow+M);
        high=max(city[N-1]-tow[0],tow[M-1]-city[0]);   //上限取city和Tower的最大距离,所以先排了个序
        low=0;
        while(high>low)
        {
            mid=(high+low)/2;
            cnt=0;
            for(int i=0,j=0;j<M&&i<N;i++)
            {
                temp=abs(city[i]-tow[j]);
                if(temp<=mid)
                {
                    cnt++;
                }
                else
                {
                    j++;
                    i--;
                }
            }
            if(cnt<N)
                low=mid+1;
            else
                high=mid;
        }
        cout<<high<<endl;
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值