Educational Codeforces Round 15 C 二分+差分数组



链接:戳这里


C. Cellular Network
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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


题意:

给出n个塔所在x轴上的坐标,给出m个充电的点所在x轴上的坐标,每个点对应的半径能覆盖[x-r,x+r]的范围

问找出一个最小的半径r,使得这m个充电的点能覆盖n座塔


思路:

二分这个半径,对于每次二分的mid,判断该半径下,能否覆盖n座塔

对于当前的r,每个充电的点能覆盖一个区间的塔[l,r]。可以通过二分x-mid和x+mid找出区间[l,r]

那么现在找出了m条线段,判断这m条线段是否包含了所有的[1,n]区间

差分数组搞搞就可以了。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
int n,m;
ll a[100100],b[100100];
int s[100100],sum[100100];
bool pd(ll r){
    mst(s,0);
    mst(sum,0);
    for(int i=1;i<=m;i++){
        int x=lower_bound(a+1,a+n+1,b[i]-r)-a;
        int y=upper_bound(a+1,a+n+1,b[i]+r)-a;
        y--;
        if(y<x) continue;
        s[x]++;
        s[y+1]--;
    }
    for(int i=1;i<=n;i++){
        sum[i]=sum[i-1]+s[i];
        if(sum[i]<=0) return false;
    }
    return true;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
    for(int i=1;i<=m;i++) scanf("%I64d",&b[i]);
    ll l=0,r=1e10,mid,ans=0;
    while(l<=r) {
        mid=(l+r)/2;
        if(pd(mid)) {
            r=mid-1;
            ans=mid;
        } else l=mid+1;
    }
    cout<<ans<<endl;
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值