EDU15 C. Cellular Network[二分]

时间:2022-12-29 23:53:54

C. Cellular Network

题意:n个城市在x轴上的坐标c[i],m个灯的坐标d[i],每个灯的射程在[d[i]-r,d[i]+r],求最小的r使得所有的城市都可以被灯覆盖

思路:单调函数,r越大肯定覆盖的概率越大.二分r

#include<bits/stdc++.h>
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
#define debug puts
#define setp cout << fixed << setprecision(3)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
const int N=1e5+5;
const int MOD=1e9+7;
const ll INF=1e18+8;
ll a[N],b[N],n,m;

bool check(ll mid){
    int cnt=0;
    int now=1;
    for(int i=1;i<=m;i++){
        ll l=b[i]-mid;
        ll r=b[i]+mid;
        while(a[now]>=l&&a[now]<=r&&now<n+1) cnt++,now++; /// 最多跑n次
    }
    return cnt==n;
}

int main(void){
    FAST_IO;
    cin >> n>> m;
    for(int i=1;i<=n;i++)   cin >> a[i];
    for(int i=1;i<=m;i++)   cin >> b[i];
    ll l=0,r=1e18,ans;
    while(l<=r){
        ll mid= l+r >> 1;
        if(check(mid))  ans=mid,r=mid-1;
        else    l=mid+1;
    }
    cout << ans << endl;


    return 0;
}
/********

*********/