codeforces 702C Cellular Network(二分+尺取)

Cellular Network


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

题目大意:有n个城市,m个灯塔,让你求一个最小的灯塔覆盖距离 r ,让n座城市都处在灯塔的照耀下。

解题思路:因为子问题具有独立性,并且答案具有单调性(一个点满足条件,他的左边或是右边都满足条件)。所以采用二分答案的做法来解决这道题。每次二分一个距离 r 。判断这个 r 能否让所有的城市被覆盖。如果可以的话,将其更新为上界,若不行将其更新为下界。中间每一次的判断过程采用尺取法,即当前城市在灯塔的覆盖范围内,继续判断下一个城市,直到出了边界。

二分法注意事项:

1、注意结束条件的写法

2、注意更新边界时区间的开闭

3、判断左右边界谁才是答案

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include<set>
#include<map>
#include<queue>

#define maxn 100005
#define inf 0x3f3f3f3f
#define ONES(x) __builtin_popcount(x)
using namespace std;

typedef long long ll ;
const double eps =1e-8;
const int mod = 1000000007;
typedef pair<int, int> P;
const double PI = acos(-1.0);
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

int n,m;
int ans;
ll a[maxn],b[maxn];
int main()
{
    //freopen("test.txt","r",stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int i;
    cin>>n>>m;
    for(i=0;i<n;i++)
        cin>>a[i];
    for(i=0;i<m;i++)
        cin>>b[i];
    ll left = 0,right = 2E9;
    ll mid = (left+right)/2;
    while(left+1<=right)
    {
        int num = 0,flag = 0;
        for(i=0;i<m;i++)
        {
            if(b[i]-mid>a[num])
            {
                flag = 1;
                break;
            }
            while(b[i]+mid>=a[num] && num<n)
                num++;
            if(num==n)
                break;
        }
        if(num!=n)
            flag = 1;
        if(flag) left = mid + 1;
        else     right = mid;
        mid = (left+right)/2;
        //cout << mid << endl;
    }

    cout << left << endl;
    return 0;
}

/*

unsigned   int   0~4294967295
int   2147483648~2147483647
unsigned long 0~4294967295
long   2147483648~2147483647
long long的最大值:9223372036854775807
long long的最小值:-9223372036854775808
unsigned long long的最大值:18446744073709551615

__int64的最大值:9223372036854775807
__int64的最小值:-9223372036854775808
unsigned __int64的最大值:18446744073709551615

*/

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值