Educational Codeforces Round 15 C Cellular Network(二分)

    xiaoxiao2021-12-14  17

    题意:有n个城市m盏灯,要你确定一个最小的R使得所有的城市都被照亮 思路:二分答案R,然后直接扫一遍check就可以了 #include<bits/stdc++.h> using namespace std; #define LL long long const int maxn = 1e5+7; LL a[maxn],b[maxn]; map<LL,int>vis; int n,m; bool check(LL mid) { int cnt = n; int pa=1,pb=1; while(pa<=n&&pb<=m) { if(a[pa]<=b[pb]+mid && a[pa]>=b[pb]-mid) pa++,cnt--; else pb++; } return cnt==0; } int main() { scanf("%d%d",&n,&m); LL maxa = 0,mina = 1e9+7; LL maxb = 0,minb = 1e9+7; for(int i = 1;i<=n;i++) { scanf("%lld",&a[i]); maxa = max(maxa,a[i]); mina = min(mina,a[i]); } for(int i = 1;i<=m;i++) { scanf("%lld",&b[i]); maxb = max(maxb,b[i]); minb = min(minb,b[i]); } sort(a+1,a+1+n); sort(b+1,b+1+m); LL l = 0,r=max(maxa-minb,maxb-mina); LL ans = 0; while(l<=r) { LL mid = (l+r)>>1; if(check(mid))ans=mid,r=mid-1; else l=mid+1; } printf("%lld\n",ans); } 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 3 2 -2 2 4 -3 0 Output 4 Input 5 3 1 5 10 14 17 4 11 15 Output 3
    转载请注明原文地址: https://ju.6miu.com/read-965704.html

    最新回复(0)