Closest point study notes

Closest point, is to concentrate to find the closest pair of points at one point.

Look at an example

AcWing119

With the help of a solution to a problem, I finally A above question.

Subject to the effect that there are n n agents and nuclear power plants, find an agent to any recent arbitrary distance of a nuclear power plant.

First, we will just point divided into two groups, with the group of distance between points is infinite, then this problem can be converted to the nearest point on the issue.

The nearest point on how to solve the problem?

The first thought is the n- 2 violence, but undoubtedly to expire.

So we need a partition to optimize it.

First, in general, the recent poor abscissa two points is small, we can divide and conquer for the horizontal axis.

 

As shown, the first refers to the abscissa keyword ranking, to all the points into two parts (as to the middle of the piece just assigned).

It is clear that the shortest distance is the shortest distance and the shortest distance from the left block to right block, of course, may be different from the points of the block, for example, in FIG. Ab.

The first two are very easy to achieve, just need to continue to divide and conquer like. The trouble is the third case.

We first determined the minimum of two prior ans, abscissa and the middle points of difference only ans less likely factor.

So we just requires the shortest distance among the points.

For these points we can do direct violence, under normal circumstances should not be great.

Of course, if the distance is greater than ANS ordinate, it is the shortest possible distance.

    	sort(b + 1, b + cnt + 1, cmp2);
	rep(i, 1, cnt)
		rep(j, i + 1, cnt){
			if(b[j].y >= b[i].y + ans) break;
			years = min (year dist (b [i], b [j]));
		}

 You can sort ordinate is the first keyword, pruning.

I put the wrong code several times

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <vector>
 7 #define rep(x, l, r) for(int x = l; x <= r; x++)
 8 #define repd(x, r, l) for(int x = r; x >= l; x--)
 9 #define clr(x, y) memset(x, y, sizeof(x))
10 #define all(x) x.begin(), x.end()
11 #define pb push_back
12 #define mp make_pair
13 #define MAXN 200005
14 #define fi first
15 #define se second
16 #define SZ(x) ((int)x.size())
17 using namespace std;
18 typedef long long ll;
19 typedef vector<int> vi;
20 typedef pair<int, int> pii;
21 const int INF = 1 << 30;
22 const int p = 1000000009;
23 int lowbit(int x){ return x & (-x);}
24 int fast_power(int a, int b){ int x; for(x = 1; b; b >>= 1){ if(b & 1) x = 1ll * x * a % p; a = 1ll * a * a % p;} return x % p;}
25 
26 int n;
27 struct node{
28     double x, y;
29     bool col;
30 }a[MAXN], b[MAXN];
31 
32 bool cmp1(node a, node b){
33     if(a.x == b.x) return a.y < b.y;
34     return a.x < b.x;
35 }
36 
37 bool cmp2(node a, node b){
38     if(a.y == b.y) return a.x < b.x;
39     return a.y < b.y;
40 }
41 
42 double dist(node a, node b){
43     if(a.col == b.col) return INF;
44     return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
45 }
46 
47 double work(int l, int r){
48     if(l == r) return INF;
49     if(l == r - 1) return dist(a[l], a[r]);
50     int mid = (l + r) >> 1;
51     double ans = min(work(l, mid), work(mid + 1, r));
52     int cnt = 0;
53     rep(i, l, r)
54         if(a[i].x >= a[mid].x - ans && a[i].x <= a[mid].x + ans) b[++cnt] = a[i];
55     sort(b + 1, b + cnt + 1, cmp2);
56     rep(i, 1, cnt)
57         rep(j, i + 1, cnt){
58             if(b[j].y >= b[i].y + ans) break;
59             ans = min(ans, dist(b[i], b[j]));
60         }
61     return ans;
62 }
63 
64 int main(){
65     int t;
66     scanf("%d", &t);
67     while(t--){
68         scanf("%d", &n);
69         rep(i, 1, n){
70             scanf("%lf%lf", &a[i].x, &a[i].y);
71             a[i].col = 0;
72         }
73         rep(i, n + 1, 2 * n){
74             scanf("%lf%lf", &a[i].x, &a[i].y);
75             a[i].col = 1;
76         }
77         n *= 2;
78         sort(a + 1, a + n + 1, cmp1);
79         double ans = work(1, n);
80         printf("%.3f\n", ans);
81     }
82     return 0;
83 }
View Code

Guess you like

Origin www.cnblogs.com/nblyz2003/p/12147898.html