P1661 diffusion (+ disjoint-set binary)

Title Description

A point every time unit diffuses a distance in the four directions, as shown in FIG.

Two points a, b communication, referred to as E (a, b), if and only if the diffusion regions a, b are common. Definition of communication block is arbitrary two points within the block u, v path will exist without fail e (u, a0), e (a0, a1), ..., e (ak, v). To n to a point on a given plane, and asked what is the earliest time that they form a communication block.

Input Format

The first line of a number n, the n lines, each line point coordinates.

[Data] scale

For 20% of the data, to meet 1≤N≤5; 1≤X [i], Y [i] ≤50;

To 100% of the data satisfies 1≤N≤50; 1≤X [i], Y [i] ≤10 ^ 9.

Output Format

A number representing the earliest time point of all the communication block is formed.

Sample input and output

Input # 1
2
0 0
5 5
Output # 1
5 

Outline of Solution: as long as two points to find the vertical distance from the horizontal + "= 2 * t would meet these two points (the multiplication point may try to find a few law) communication block first thought then two disjoint-set time points Solving on the line

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define eps 1e-6
 5 using namespace std;
 6 typedef long long ll;
 7 
 8 int n;
 9 ll x[55],y[55];
10 ll arr[55];
11 
12 ll find_root(ll x){
13     return arr[x]==x?x:arr[x]=find_root(arr[x]);
14 }
15 
16 boolCheck (LL NUM) {
 . 17      int ANS = 0 ;
 18 is      for ( int I = . 1 ; I <= n-; I ++) ARR [I] = I;
 . 19      for ( int I = . 1 ; I <= n-; I ++ ) {
 20 is          for ( int J = I + . 1 ; J <= n-; J ++ ) {
 21 is              IF (ABS (X [I] the -X-[J]) + ABS (Y [I] -Y [J]) <= 2 * NUM ) { // within this time can num possible to meet two points 
22 is                  LL = XX find_root (I);
 23 is                  LL = YY find_root (J);
 24                 arr[xx]=yy;    ///要找根啊
25             }
26         }
27     }
28     for(int i=1;i<=n;i++){
29         if(arr[i]==i) ans++;
30     }
31     if(ans==1) return true;
32     else return false;
33 }
34 
35 void Dichotomy(){
36     ll left=0,right=1e9;
37     ll ans;
38     while(left<=right){
39         ll mid=left+right>>1;
40         if(check(mid)) right=mid-1,ans=mid;
41         else left=mid+1;
42     }
43     printf("%lld\n",ans);
44 }
45 
46 int main(){
47     scanf("%d",&n);
48     for(int i=1;i<=n;i++) scanf("%lld%lld",&x[i],&y[i]);
49     Dichotomy();
50     return 0;
51 }
View Code

 

Guess you like

Origin www.cnblogs.com/qq-1585047819/p/11256837.html