Cf Edu 15 C 城市与信号塔[二分]
来源:互联网 发布:去人声软件 编辑:程序博客网 时间:2024/06/05 09:25
题目连接
http://codeforces.com/contest/702/problem/C
Description
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.
Sample Input
5 3
1 5 10 14 17
4 11 15
Sample Output
3
题意
一条直线上有城市和信号塔,保证每个城市都能接受到信号,每个城市与最近的信号塔距离为d,求出最小的ansd,保证所有的城市与最近的信号塔距离都是小于等于ansd的。
题解
对信号塔的位置进行二分,求出与每一个城市最近的信号塔,取其中的最大值,注意特判当信号塔数为1时,取该塔与最左与最右的较大值。
代码
#include<bits/stdc++.h>using namespace std;const int maxn=100005;int a[maxn];int b[maxn];int main(){ int n,m; scanf("%d%d",&n,&m); for(int i=0;i<n;i++) { scanf("%d",&a[i]); } for(int i=0;i<m;i++) { scanf("%d",&b[i]); } int ans=0; if(m==1){printf("%d",max(abs(a[0]-b[0]),abs(b[0]-a[n-1])));return 0;} for(int i=0;i<n;i++) { int le,ri,mi,tt; le=0;ri=m-1; while(le<ri) { mi=(le+ri)/2; tt=abs(a[i]-b[mi]); if(b[mi]>a[i])ri=mi; else le=mi; if(abs(le-ri)==1) { tt=min(abs(a[i]-b[le]),abs(a[i]-b[ri])); break; } } ans=max(ans,tt); } printf("%d\n",ans); return 0;}
- Cf Edu 15 C 城市与信号塔[二分]
- Cf Edu 15 C Cellular Network 二分
- cf 371c 二分
- cf 551 C(二分)
- cf 689 C(二分)
- CF 493C 二分
- cf-edu#5-C The Labyrinth-dfs记忆化搜搜
- 二分 cf 343c div1
- Cf 361div2 C [二分]
- CF EDU #7
- CF Edu 15 B 求和二的幂次
- cf#336-C - Chain Reaction-二分
- CF 551c 二分搜索+思维题
- Edu CF #R8(ABC)
- CF 二分
- CF 15C
- CF EDU #8 D - Magic Number
- CF#262(div2) C——Present(二分)
- DNS原理入门
- HomeBrew for mac to install node npm
- CodeForce 628D Magic Numbers( 数位DP )
- AVPlayer自定制视频播放器(1)——视频播放器基本实现
- Sql中Insert添加语句如何不重复
- Cf Edu 15 C 城市与信号塔[二分]
- 洛谷 P2530 [SHOI2001]化工厂装箱员
- leetcode #4: Median of Two Sorted Arrays
- Struts2学习之旅(2)
- Java容器五:Vector
- iOS简单使用description方法(参考大神博客+自己想法)
- C++之引用
- 淘宝API示例
- [基础算法]递归二分算法