題目鏈接:
http://poj.org/problem?id=3714
題目描述:
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 integers 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
題目大意:
有兩個點集,求集合不同的點的最小距離
思路:
分治,和經典最近點對一樣,以x坐標排序,划中線分成兩堆,遞歸求每堆的最近距離d,然后對兩堆之間橫坐標為 x[mid] - d 和 x[mid] + d 之間的點暴力看是否有比 d 小的距離
據說數據比較水可以暴力
注意會爆int
代碼:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 using namespace std; 7 8 typedef long long LL; 9 10 const LL INF = 1000000000000; 11 const int N = 100010; 12 13 struct Node { 14 LL x, y; 15 int id; 16 Node(LL x = 0, LL y = 0, int id = 0) :x(x), y(y), id(id) {} 17 const bool operator < (const Node A) const { 18 return x == A.x ? y < A.y : x < A.x; 19 } 20 }no[2 * N]; 21 22 int n; 23 24 double dis(int a, int b) { 25 return sqrt((double)((no[a].x - no[b].x)*(no[a].x - no[b].x) + (no[a].y - no[b].y)*(no[a].y - no[b].y))); 26 } 27 28 double solve(int l, int r) { 29 if (l == r)return INF; 30 int mid = (l + r) >> 1; 31 double a = solve(l, mid); 32 double b = solve(mid + 1, r); 33 double d = min(a, b); 34 for (int i = mid; i >= l; --i) { 35 if (no[mid].x - no[i].x > d)break; 36 for (int j = mid + 1; j <= r; ++j) { 37 if (no[j].x - no[i].x > d)break; 38 double tmp = dis(i, j); 39 if (no[i].id != no[j].id&&tmp < d)d = tmp; 40 } 41 } 42 return d; 43 } 44 45 int main() { 46 int t; 47 scanf("%d", &t); 48 while (t--) { 49 scanf("%d", &n); 50 for (int i = 0; i < n; ++i) { 51 scanf("%lld%lld", &no[i].x, &no[i].y); 52 no[i].id = 1; 53 } 54 for (int i = 0; i < n; ++i) { 55 scanf("%lld%lld", &no[i + n].x, &no[i + n].y); 56 no[i + n].id = 2; 57 } 58 sort(no, no + 2 * n); 59 double ans = solve(0, 2 * n - 1); 60 printf("%.3lf\n", ans); 61 } 62 }