CF702C Cellular Network [half]

Title Description

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.

Input Format

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.

Output Format

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

Translation of the meaning of problems

Gives the n cities on the straight line position (x-coordinate) and the position (x-coordinate) on the same straight line of m cell tower. All the tower works the same way - they provide the cellular network to all cities, these cities are located in order to cover the distance from the cellular network tower does not exceed r's.

Your task is to find that each city can be minimized r value of cellular network coverage that each city is in the range of distances r there is at least one cellular tower.

If r = 0, it provides the only tower in the position of the cellular network. A cellular network tower can provide any number of cities, but all these cities must not exceed the distance r of the distance tower.

Input Format

The first line contains two positive integers n and m, n represent cities and m have cellular towers.

The second line contains n integers A [. 1], A [2] ... A n , denotes the location of each city (x-coordinate)

The third line contains integers m B [. 1], B [2] ... B m , indicates the position of each cell tower (x-coordinate)

Note that allowing a plurality of the same city, or cellular tower location.

Output Format

The minimum output r, such that each city is covered by a cellular network.

data range

1<=n,m<=10^5

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

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

Sample input and output

Input # 1
3 2
-2 2 4
-3 0
Output # 1
4
Input # 2
5 3
1 5 10 14 17
4 11 15
Output # 2
3 

ideas
  note the minimum r is 0, and then two.

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 ;
}

Guess you like

Origin www.cnblogs.com/orangeko/p/12452907.html