Raid

题目描述

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?

输入

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.

输出

For each test case output the minimum distance with precision of three decimal placed in a separate line.

样例输入

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

样例输出

1.414
0.000

分析

求平面最近点对。
用分治的思想,按横坐标分成左右两部分,分别求出左右两部分的最小距离d。然后剩下要处理一个点在左侧,一个点在右侧的情况。假如一个点在左侧,那么我们只需要考虑右侧在分割线距离d之内,并且与左侧点竖直距离不超过d的点。这样每个左侧的点要比较的右侧点的个数至多为6.

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <vector>
#include <ctime>
using namespace std;
typedef long long ll;
const int maxn=200050;
const ll inf=3e18;
struct Point {
	int x,y,id;
}a[maxn];
ll dis(const Point&a,const Point&b) {
	ll x=a.x,y=a.y,u=b.x,v=b.y;
	return (ll)(x-u)*(x-u)+(y-v)*(y-v);
}
bool cmpx(const Point&a,const Point&b) {
	return a.x<b.x;
}
bool cmpy(const Point&a,const Point&b) {
	return a.y<b.y;
}
int n;
Point L[maxn],R[maxn];
ll Min(int l,int r) {
	if(l>=r) return inf;
	if(l==r-1) return (a[l].id==a[r].id?inf:dis(a[l],a[r]));
	int mid=(l+r)>>1;
	ll d1=Min(l,mid),d2=Min(mid+1,r);
	ll d=min(d1,d2);
	ll dd=sqrt(d)+1;
	int c1=0,c2=0;
	for(int i = l; i <= mid; ++i)
		if(a[mid+1].x-a[i].x <= dd) L[c1++]=a[i];
	for(int i = mid+1; i <= r; ++i)
		if(a[i].x-a[mid].x <= dd) R[c2++]=a[i];
	sort(L,L+c1,cmpy);
	sort(R,R+c2,cmpy);
	int j=0,k=-1;
	for(int i = 0; i < c1; ++i) {
		while(k+1<c2&&R[k+1].y<=dd+L[i].y) ++k;
		while(j<c2&&R[j].y<-dd+L[i].y) ++j;
		for(int t = j; t <= k; ++t) {
			if(L[i].id!=R[t].id) d=min(d,dis(L[i],R[t]));
		}
	}
	return d;
}
int main() {
	int t;
	scanf("%d", &t);
	while(t--) {
		scanf("%d", &n);
		for(int i = 0; i < n; ++i) scanf("%d%d", &a[i].x,&a[i].y),a[i].id=0;
		for(int i = n; i < 2*n; ++i) scanf("%d%d", &a[i].x,&a[i].y),a[i].id=1;
		sort(a,a+n*2,cmpx);
		printf("%.3Lf
", sqrt((long double)Min(0,n*2-1)));
	}
}
原文地址:https://www.cnblogs.com/sciorz/p/9342076.html