本文主要是介绍codeforces 702C Cellular Network,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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.
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.
Print minimal r so that each city will be covered by cellular network.
二分查找就行了
AC代码:
# include <stdio.h>
# include <string.h>
# include <algorithm>
# include <stdlib.h>
# include <math.h>
using namespace std;
int a[100010];
int b[100010];
int main(){int r=0, i, j, k;int n, m;scanf("%d%d", &n, &m);for(i=0; i<=n-1; i++){scanf("%d", &a[i]);}for(i=0; i<=m-1; i++){scanf("%d", &b[i]);}for(i=0; i<=n-1; i++){int pos=lower_bound(b, b+m, a[i])-b;if(pos==m){r=max(r, abs(a[i]-b[m-1]));}else if(pos==0){r=max(r, abs(a[i]-b[pos]));}else{int Min=min(abs(a[i]-b[pos-1]), abs(a[i]-b[pos]));r=max(Min, r);}}printf("%d", r);return 0;
}
这篇关于codeforces 702C Cellular Network的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!