CF702C Cellular Network【二分】

题目描述

You are given nn points on the straight line — the positions ( xx -coordinates) of the cities and mm points on the same line — the positions ( xx -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 rr from this tower.

Your task is to find minimal rr 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 rr .

If r=0r=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 rr from this tower.

输入格式

The first line contains two positive integers nn and mm ( 1<=n,m<=10^{5}1<=n,m<=105 ) — the number of cities and the number of cellular towers.

The second line contains a sequence of nn integers a_{1},a_{2},...,a_{n}a1,a2,...,an ( -10^{9}<=a_{i}<=10^{9}109<=ai<=109 ) — the coordinates of cities. It is allowed that there are any number of cities in the same point. All coordinates a_{i}ai are given in non-decreasing order.

The third line contains a sequence of mm integers b_{1},b_{2},...,b_{m}b1,b2,...,bm ( -10^{9}<=b_{j}<=10^{9}109<=bj<=109 ) — the coordinates of cellular towers. It is allowed that there are any number of towers in the same point. All coordinates b_{j}bj are given in non-decreasing order.

输出格式

Print minimal rr so that each city will be covered by cellular network.

题意翻译

在直线上给出n个城市的位置(x坐标)和在同一直线上的m个蜂窝塔的位置(x坐标)。所有的塔都以同样的方式工作——它们为所有城市提供蜂窝网络,这些城市位于离塔不超过r的距离处才能被蜂窝网络覆盖。

你的任务是找出使得每个城市都能被蜂窝网络覆盖的最小r值,即每个城市在距离r的范围内至少有一个蜂窝塔。

如果r=0,则塔仅为其所在的位置提供蜂窝网络。一个塔可以为任意数量的城市提供蜂窝网络,但是所有这些城市都必须在距离塔不超过r的距离上。

输入格式

第一行包含两个正整数n和m,表示有n个城市与m个蜂窝塔。

第二行包含n个整数a[1],a[2]...an,表示每个城市的位置(x坐标)

第三行包含m个整数b[1],b[2]...bm,表示每个蜂窝塔的位置(x坐标)

注意,允许多个城市或蜂窝塔位置相同。

输出格式

输出最小的r,使得每个城市都被蜂窝网络覆盖。

数据范围

1<=n,m<=10^5

-10^9<=a[i]<=10^9

-10^9<=b[j]<=10^9

输入输出样例

输入 #1
3 2
-2 2 4
-3 0
输出 #1
4
输入 #2
5 3
1 5 10 14 17
4 11 15
输出 #2
3

思路
  注意一下 r 最小是可以为0的, 然后二分。

CODE
#include  < bits/stdc++.h >
#define  dbg ( x ) cout  << #x  <<  " = "  << x  << endl
#define  eps  1e - 8
#define  pi  acos ( - 1.0 )

using  namespace  std ;
typedef  long  long LL ;

const  int inf  =  0x3f3f3f3f ;

template < class  T > inline  void  read ( T  & res )
{
     char c ;T flag = 1 ;
     while ((c = getchar ()) < ' 0 ' ||c > ' 9 ' ) if (c == ' - ' )flag =- 1 ;res =c - ' 0 ' ;
     while ((c = getchar ()) >= ' 0 ' &&c <= ' 9 ' )res =res * 10 +c - ' 0 ' ;res *=flag ;
}

namespace  _buff  {
     const  size_t BUFF  =  1  <<  19 ;
     char  ibuf [BUFF ],  *ib  = ibuf ,  *ie  = ibuf ;
     char  getc ()  {
         if  (ib  == ie )  {
            ib  = ibuf ;
            ie  = ibuf  +  fread (ibuf ,  1 , BUFF , stdin );
         }
         return ib  == ie  ?  - 1  :  *ib ++ ;
     }
}

int  qread ()  {
     using  namespace  _buff ;
     int ret  =  0 ;
     bool pos  =  true ;
     char c  =  getc ();
     for  (;  (<  ' 0 '  || c  >  ' 9 ' )  && c  !=  ' - ' ; c  =  getc ())  {
         assert ( ~c );
     }
     if  (==  ' - ' )  {
        pos  =  false ;
        c  =  getc ();
     }
     for  (; c  >=  ' 0 '  && c  <=  ' 9 ' ; c  =  getc ())  {
        ret  =  (ret  <<  3 )  +  (ret  <<  1 )  +  (^  48 );
     }
     return pos  ? ret  :  -ret ;
}

const  int maxn  =  1e5  +  7 ;

LL n , m , r ;

LL a [maxn ], b [maxn ];
LL maxx  = INT_MIN , minn  = INT_MAX ;

LL  Max ( LL  a ,  LL  b )  {
     if (> b )  {
         return a ;
     }
     return b ;
}

LL  Min ( LL  a ,  LL  b )  {
     if (< b )  {
         return a ;
     }
     return b ;
}

bool  check ( LL  r )  {
     int cnt  =  1 ;
     for  (  int i  =  1 ; i  <= m ;  ++)  {
         while ( a [cnt ]  >=  b [i ] -&&  a [cnt ]  <=  b [i ]  + r  && cnt  <= n )  {
             ++cnt ;
         }
         if (cnt  -  1  == n )  {
             return  1 ;
         }
     }
     return  0 ;
}

int  main ()
{
     read (n );
     read (m );
     for  (  int i  =  1 ; i  <= n ;  ++)  {
         read ( a [i ]);
        maxx  =  Max (maxx ,  a [i ]);
        minn  =  Min (minn ,  a [i ]); 
     }
     for  (  int i  =  1 ; i  <= m ;  ++)  {
         read ( b [i ]);
        maxx  =  Max (maxx ,  b [i ]);
        minn  =  Min (minn ,  b [i ]);
     }  

    LL l  =  0 , r  = maxx  -  Min (minn ,  0 );
    LL mid  =  (+ r )  >>  1 ;
     while (<= r )  {
        mid  =  (+ r )  >>  1 ;
         if ( check (mid ))  {
            r  = mid  -  1 ;
         }
         else  {
            l  = mid  +  1 ;
         }
     }
    cout  << l  << endl ;
     return  0 ;
}

猜你喜欢

转载自www.cnblogs.com/orangeko/p/12452907.html
今日推荐