欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【POJ 3714】Raid(平面分治)

程序员文章站 2022-03-02 23:06:01
...

Raid

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 16419   Accepted: 4581

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

这道题可以算是最小距离点对的改编版,我们可以在最小点对的基础上给两个点集合进行标记,把同一个集合内部的两点设置为无穷大,然后直接用最小点对算法就可以了。

#include<iostream>
#include<algorithm>
#include<string.h>
#include<cstdio>
#include<math.h>
using namespace std;
#define maxn 1000500
#define INF 1e50
struct point 
{
	double x,y;
	int flag;
}s[maxn],re[maxn];
bool cmpx(point a,point b)
{
	return a.x<b.x;
}
bool cmpy(point a,point b)
{
	return a.y<b.y;
}
double dis(point a,point b){
	if(a.flag==b.flag) return INF;
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

double ab(double a){
	return a>=0?a:-a;
}

double solve(int l,int r){
	if(l==r) return INF;
	int mid=(l+r)>>1;
	int cnt=0;
	double ans=min(solve(l,mid),solve(mid+1,r));
	for(int i=l;i<=r;i++){
		if(ab(s[i].x-s[mid].x)<=ans){
			re[cnt++]=s[i];
		}
	}
	sort(re,re+cnt,cmpy);
	for(int i=0;i<cnt;i++){
		for(int j=i+1;j<cnt;j++){
			if(re[j].y-re[i].y>ans) break;
			else ans=min(ans,dis(re[i],re[j]));
		}
	}
	return ans;
}

int main(){
	int n,t;
	scanf("%d",&t);
	while(t--){
	memset(re,0,sizeof(re));
	memset(s,0,sizeof(s));
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%lf%lf",&s[i].x,&s[i].y);
		s[i].flag=0;
	}
	for(int i=n;i<2*n;i++){
		scanf("%lf%lf",&s[i].x,&s[i].y);
		s[i].flag=1;
	}
	sort(s,s+2*n,cmpx);
	printf("%.3lf\n",solve(0,2*n-1));
}
	return 0;
}