您的位置:首页 > 其它

Educational Codeforces Round 15 C 二分+差分数组

2016-08-01 15:12 281 查看
链接:戳这里

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个塔所在x轴上的坐标,给出m个充电的点所在x轴上的坐标,每个点对应的半径能覆盖[x-r,x+r]的范围

问找出一个最小的半径r,使得这m个充电的点能覆盖n座塔

思路:

二分这个半径,对于每次二分的mid,判断该半径下,能否覆盖n座塔

对于当前的r,每个充电的点能覆盖一个区间的塔[l,r]。可以通过二分x-mid和x+mid找出区间[l,r]

那么现在找出了m条线段,判断这m条线段是否包含了所有的[1,n]区间

差分数组搞搞就可以了。

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
int n,m;
ll a[100100],b[100100];
int s[100100],sum[100100];
bool pd(ll r){
mst(s,0);
mst(sum,0);
for(int i=1;i<=m;i++){
int x=lower_bound(a+1,a+n+1,b[i]-r)-a;
int y=upper_bound(a+1,a+n+1,b[i]+r)-a;
y--;
if(y<x) continue;
s[x]++;
s[y+1]--;
}
for(int i=1;i<=n;i++){
sum[i]=sum[i-1]+s[i];
if(sum[i]<=0) return false;
}
return true;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
for(int i=1;i<=m;i++) scanf("%I64d",&b[i]);
ll l=0,r=1e10,mid,ans=0;
while(l<=r) {
mid=(l+r)/2;
if(pd(mid)) {
r=mid-1;
ans=mid;
} else l=mid+1;
}
cout<<ans<<endl;
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: