Raid
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
题意:求两个点集的最近点对。
分析:先把所有点分成两部分,求出这两个部分的最近点对距离l1,l2,找出所有横纵坐标距离小于Min(l1,l2)的点,然后更新最小值。
代码
#include <cstdio>
#include <cmath>
#include <algorithm>
#define inf 1e9
#define N 500005
using namespace std;
struct arr
{
double x, y;
int z;
}a[N];
int n,T,num[N];
int cmp1(arr p, arr q)
{
if (p.x == q.x) return p.y < q.y;
return p.x < q.x;
}
int cmp2(int p, int q)
{
if (a[p].y == a[q].y) return a[p].x < a[q].x;
return a[p].y < a[q].y;
}
double fsqr(double x) {return x*x;}
double dist(arr p, arr q)
{
return sqrt(fsqr(p.x - q.x) + fsqr(p.y - q.y));
}
double find(int l, int r)
{
double tmp = 1e50;
if (l == r) return tmp;
if (l + 1 == r) return a[l].z==a[r].z ? tmp : dist(a[l],a[r]);
int mid = (l + r) >> 1;
tmp = min(find(l,mid), find(mid + 1,r));
int cnt = 0;
for (int i = l; i <= r; i++)
if (abs(a[i].x - a[mid].x) <= tmp) num[++cnt] = i;
sort(num + 1,num + cnt + 1,cmp2);
for (int i = 1; i <= cnt; i++)
for (int j = i+1; j <= cnt; j++)
{
if (abs(a[num[j]].y - a[num[i]].y) >= tmp) break;
if (a[num[i]].z != a[num[j]].z) tmp = min(tmp, dist(a[num[i]], a[num[j]]));
}
return tmp;
}
int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%lf%lf", &a[i].x, &a[i].y);
a[i].z = 1;
}
for (int i = 1; i <= n; i++)
scanf("%lf%lf", &a[i+n].x, &a[i+n].y);
sort(a + 1,a + 2*n + 1,cmp1);
printf("%.3f\n", find(1,2*n));
}
}