版权声明:本文为博主原创文章,未经博主允许不得转载。
题目链接:http://codeforces.com/problemset/problem/702/C点击打开链接
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
解题感悟:
刚刚开始拿到这道题的时候,我就怕了,分析了第一个样例,很快就得出了答案,可是自己又不想动脑子就望文生义了一下,哇,这题一定很难,肯定要考虑很多条件,二分也不好做。好的,就这样,我这个水量十足的弱弱,就放弃了思考这道题。吼!现在再来看看这道题,还是比较简单的二分题,但是后来也不是自己想出来的,是朋友给我讲的思路以及看的代码。emmm,现在我都忘得差不多了,并且刚好最近都在写关于最少的思想(有点类似贪心吧),那么我就在来靠自己的思维好好想想这道题。来,扶我起来,我还能学。
言归正传:
解题思路:
就是要找出n个城市,每个城市距离最近的塔,然后再在这n个最近距离中取最小值,即可。
代码实现:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;const int maxn = 2 * 1e9;
int city[100005],tower[100005];int main()
{int i,j,n,m,ans;while(~scanf("%d %d",&n,&m)){ans = 0;for(i = 1;i <= n; i++)scanf("%d",&city[i]);for(j = 1;j <= m; j++)scanf("%d",&tower[j]);for(i = 1;i <= n; i++){int minn = maxn;int left = 1,right = m;while(left <= right){int mid = (left + right) / 2;if(tower[mid] == city[i]){minn = 0;break;}else if(tower[mid] > city[i]){if(tower[mid] - city[i] < minn)minn = tower[mid] - city[i];right = mid - 1;}else{if(city[i] - tower[mid] < minn)minn = city[i] - tower[mid];left = mid + 1;}}if(ans < minn)ans = minn;}printf("%d\n",ans);}return 0;
}