codeforces 702C 二分查找

3 篇文章 0 订阅
1 篇文章 0 订阅
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
Copy
3 2
-2 2 4
-3 0
output
Copy
4
input
Copy
5 3
1 5 10 14 17
4 11 15
output
Copy
3

   一道二分查找的,训练的时候疯狂wa,没有注意到二分查找的范围除了要判断大于小于之外,还要判断等于。另外,需要注意二分查找的条件的l <= r。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include  <algorithm>
using namespace std;
const int p = 100009; 
typedef long long LL;
int a[p],b[p];
int main ()
{
int n,m;
scanf("%d%d",&n,&m);
int cnt;
for (int i = 0; i < n; i++) scanf("%d",&a[i]);
for (int i = 0; i < m; i++) scanf("%d",&b[i]);
cnt = 0;
if (m == 1){
cnt = max(abs(a[0]-b[0]),abs(a[n-1]-b[0]));
printf("%d\n",cnt);
return 0;
}
for (int i = 0; i < n; i++){
int l = 0,r = m-1;
int mid;
if (a[i] < b[0])
cnt = max(cnt,b[0]-a[i]);
else if (a[i] > b[m-1])
cnt= max(cnt,a[i]-b[m-1]);
else  {
while (l <= r){
mid = (l+r)/2;
if (b[mid] < a[i])
l = mid+1;
else
r = mid-1;
}
if (abs(b[r] - a[i]) < abs(b[l] - a[i]))
cnt = max(cnt,abs(b[r]-a[i]));
else cnt = max(cnt,abs(b[l]-a[i]));
}
}
printf("%d\n",cnt);
}
/************************************************ 
┆  ┏┓   ┏┓ ┆    
┆┏┛┻━━━┛┻┓ ┆ 
┆┃       ┃ ┆ 
┆┃   ━   ┃ ┆ 
┆┃ ┳┛ ┗┳ ┃ ┆ 
┆┃       ┃ ┆  
┆┃   ┻   ┃ ┆ 
┆┗━┓    ┏━┛ ┆ 
┆  ┃    ┃  ┆       
┆  ┃    ┗━━━┓ ┆ 
┆  ┃  AC代马   ┣┓┆ 
┆  ┃           ┏┛┆ 
┆  ┗┓┓┏━┳┓┏┛ ┆ 
┆   ┃┫┫ ┃┫┫ ┆ 
┆   ┗┻┛ ┗┻┛ ┆       
*

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值