poj 3714 Raid(平面最近点对)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 7473 |
Accepted: 2221 |
Description
After successive failures in the battles against the Union, the Empire retreated to its last stronghold. Depending on its powerful defense system, the Empire repelled the six waves of Union's attack. After several sleepless nights of thinking, Arthur, General of the Union, noticed that the only weakness of the defense system was its energy supply. The system was charged by N nuclear power stations and breaking down any of them would disable the system.
The general soon started a raid to the stations by N special agents who were paradroped into the stronghold. Unfortunately they failed to land at the expected positions due to the attack by the Empire Air Force. As an experienced general, Arthur soon realized that he needed to rearrange the plan. The first thing he wants to know now is that which agent is the nearest to any power station. Could you, the chief officer, help the general to calculate the minimum distance between an agent and a station?
Input
The first line is a integer T representing the number of test cases. Each test case begins with an integer N (1 ≤ N ≤ 100000). The next N lines describe the positions of the stations. Each line consists of two i
ntegers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the station. The next following N lines describe the positions of the agents. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the agent.
Output
For each test case output the minimum distance with precision of three decimal placed in a separate line.
Sample Input
2 4 0 0 0 1 1 0 1 1 2 2 2 3 3 2 3 3 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Sample Output
1.414 0.000
Source
1对原数组依据x左标从小到大排序。
2二分数组,左边求出最小值,右边求出最小值,我们求最小的。
3找出对于左右两边的可能小于当前最小值的最近点对,更新最小值。
这题目需要区分一下点,让我们求的是闪兵到任意一个核电站的最短距离,加一个标志就可以了。(若是同一类的点就规定距离为oo!!)
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cmath> 5 #include <cstring> 6 7 #define DX(x) ((x) * (x)) 8 using namespace std; 9 10 11 const int MAX = 200000 + 10; 12 const double INF = 10e100; 13 struct Point 14 { 15 double x, y; 16 int index,flag;//flag分类!! 17 }A[MAX], B[MAX], C[MAX]; 18 19 bool compX(const Point& a, const Point& b) 20 { 21 if(a.x == b.x) 22 return a.y < b.y; 23 return a.x < b.x; 24 } 25 bool compY(const Point& a, const Point& b) 26 { 27 if(a.y == b.y) 28 return a.x < b.x; 29 return a.y < b.y; 30 } 31 32 inline double getMin(double a, double b) 33 { 34 return a < b ? a : b; 35 } 36 37 double getDist(Point& a, Point& b) 38 { 39 if(a.flag==b.flag)return INF;//同类规定距离为oo!! 40 return sqrt(DX(a.x - b.x) + DX(a.y - b.y)); 41 } 42 43 void merge(Point p[], Point q[], int s, int m, int t) 44 { 45 int i, j, k; 46 for(i = s, j = m + 1, k = s; i <= m && j <=t;) 47 { 48 if(q[i].y > q[j].y) 49 p[k++] = q[j], j++; 50 else 51 p[k++] = q[i], i++; 52 } 53 while(i <= m) 54 p[k++] = q[i++]; 55 while(j <= t) 56 p[k++] = q[j++]; 57 } 58 59 double closest(Point a[], Point b[], Point c[], int p, int q) 60 { 61 if(q - p == 1) 62 return getDist(a[p], a[q]); 63 if(q - p == 2) 64 { 65 double x1 = getDist(a[p], a[q]); 66 double x2 = getDist(a[p + 1], a[q]); 67 double x3 = getDist(a[p], a[p + 1]); 68 return getMin(x1, getMin(x2, x3)); 69 } 70 int i, j, k, m = (p + q) / 2; 71 double d1, d2; 72 for(i = p, j = p, k = m + 1; i <= q; ++i) 73 { 74 if(b[i].index <= m) 75 c[j++] = b[i]; 76 else 77 c[k++] = b[i]; 78 } 79 d1 = closest(a, c, b, p, m); 80 d2 = closest(a, c, b, m + 1, q); 81 double dm = getMin(d1, d2); 82 merge(b, c, p, m, q); 83 for(i = p, k = p; i <= q; ++i) 84 { 85 if(fabs(b[i].x - b[m].x) < dm) 86 c[k++] = b[i]; 87 } 88 for(i = p; i < k; ++i) 89 for(j = i + 1; j < k && c[j].y - c[i].y < dm; ++j) 90 { 91 double temp = getDist(c[i], c[j]); 92 if(temp < dm) 93 dm = temp; 94 } 95 return dm; 96 } 97 int main() 98 { 99 //freopen("in.txt", "r", stdin); 100 int T,n; 101 scanf("%d",&T); 102 while( T--) 103 { 104 scanf("%d", &n); 105 for(int i = 0; i < n; ++i) 106 scanf("%lf%lf", &A[i].x, &A[i].y),A[i].flag=0; 107 for(int i = 0; i < n; ++i) 108 scanf("%lf%lf", &A[i+n].x, &A[i+n].y),A[i+n].flag=1; 109 sort(A, A + 2*n, compX); 110 for(int i = 0; i < 2*n; ++i) 111 A[i].index = i; 112 memcpy(B, A, 2*n * sizeof(B[0])); 113 sort(B, B + 2*n, compY); 114 double d = closest(A, B, C, 0, 2*n - 1); 115 printf("%.3f\n", d); 116 } 117 return 0; 118 }