Raid POJ - 3714 分治最近点对模板

复杂度大约是nloglog

//#include<bits/stdc++.h>  
//#pragma comment(linker, "/STACK:1024000000,1024000000")   
#include<stdio.h>  
#include<algorithm>  
#include<queue>  
#include<string.h>  
#include<iostream>  
#include<math.h>                    
#include<set>  
#include<map>  
#include<vector>  
#include<iomanip> 
#include<bitset>
using namespace std;         //

#define ll long long  
#define pb push_back  
#define FOR(a) for(int i=1;i<=a;i++) 
#define sqr(a) (a)*(a)
#define dis(a,b) sqrt(sqr(a.x-b.x)+sqr(a.y-b.y))
ll qp(ll a,ll b,ll mod){
	ll t=1;while(b){if(b&1)t=t*a%mod;b>>=1;a=a*a%mod;}return t;
}
struct DOT{ll x;ll y;};
inline void read(int &x){int k=0;char f=1;char c=getchar();for(;!isdigit(c);c=getchar())if(c=='-')f=-1;for(;isdigit(c);c=getchar())k=k*10+c-'0';x=k*f;} 
const int dx[4]={0,0,-1,1};
const int dy[4]={1,-1,0,0};
const int inf=0x3f3f3f3f;  
const ll mod=1e9+7;

const int maxn=2e5+10;
int n;
struct NODE{
	double x,y;
	bool bel;
}a[maxn],tmp[maxn];

bool cmpx(NODE a,NODE b){return a.x<b.x;}
bool cmpy(NODE a,NODE b){return a.y<b.y;}

double make(int l,int r){
	if(l==r)return 9e18;
	int m=l+r>>1;
	int cnt=0;
	double ans=min(make(l,m),make(m+1,r));
	for(int i=l;i<=r;i++){
		if(fabs(a[i].x-a[m].x)<=ans)
			tmp[++cnt]=a[i];
	}
	sort(tmp+1,tmp+1+cnt,cmpy);
	for(int i=1;i<=cnt;i++){
		for(int j=i+1;j<=cnt;j++){
			if(tmp[j].y-tmp[i].y>ans)break;
			if(tmp[i].bel==tmp[j].bel)continue;
			ans=min(ans,dis(tmp[i],tmp[j]));
		}
	}
	return ans;
}

int main(){
	int T;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].bel=0;
		}
		for(int i=1+n;i<=n+n;i++){
			scanf("%lf%lf",&a[i].x,&a[i].y);
			a[i].bel=1;
		}
		sort(a+1,a+2*n+1,cmpx);
		printf("%.3f\n",make(1,2*n));
	}
}

posted @ 2018-02-11 18:00  Drenight  阅读(215)  评论(0编辑  收藏  举报