CodeForces

来源:互联网 发布:万科金域名城怎么样 编辑:程序博客网 时间:2024/06/10 16:17

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个城市,m个灯塔的位置,求最小的灯塔覆盖半径,使得灯塔能覆盖所有城市

题目答案具有二分性质,直接二分ANS
然后O(n+m)判断是否能全部覆盖

主要L,R需要开LL,不然答案在大于1e9的时候,L+R会爆掉int

#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;typedef long long LL;const int maxn=1e5+5;int a[maxn],b[maxn],n,m;inline bool check(LL x){  int l=1,r=1;  while(l<=n)  {    while(r<=m&&(b[r]-x>a[l]||b[r]+x<a[l]))++r;    if(r>m)return 0;    ++l;  }  return 1;}int main(){  while(~scanf("%d%d",&n,&m))  {    for(int i=1;i<=n;++i)scanf("%d",a+i);    for(int i=1;i<=m;++i)scanf("%d",b+i);    LL L=0,R=2000000002;    while(L+1<R)    {      LL mid=(L+R)>>1;      if(check(mid))R=mid;      else L=mid;    }    if(check(L))printf("%lld\n",L);    else printf("%lld\n",R);  }  return 0;}
原创粉丝点击