HDU 1007 Quoit Design

最近点对 。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <math.h>

using namespace std;

#define sf scanf
#define pf printf
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define dbg(x) cerr << "__LINE__" << " : " << #x << " : " << (x) << endl
#define nMax 1000100


typedef double D;

D const eps = 1e-8;

int dcmp(double x) {
	if( x>-eps && x<eps) return 0;
	return x > 0 ? 1 : -1;
}

struct P{
	double x,y;
	P () {};
	P (double x,double y):x(x),y(y){};
	void read () { sf("%lf%lf",&x,&y); }
	D dist() { return sqrt(x*x + y*y); }
};
P operator + (const P& u,const P& v) { return P(u.x+v.x,u.y+v.y); }
P operator - (P const& u,P const& v) { return P(u.x-v.x,u.y-v.y); }
P operator * (P const& u, double k ) { return P(u.x * k,u.y * k); }
P operator / (P const& u, double k ) { return P(u.x / k,u.y / k); }
D operator * (P const& u,P const& v) { return u.x*v.y - u.y*v.x;  }
D operator ^ (P const& u,P const& v) { return u.x*v.x + u.y*v.y;  }


int n;
P a[nMax];
int s[nMax];

int cmpx(int i,int j) {
	return a[i].x < a[j].x ;
}
int cmpy(int i,int j) {
	return a[i].y < a[j].y;
}
double min_dist(int l,int r) {
	double ans = 1e10;
	if(r-l < 20) {
		for(int q=l;q<r;q++) 
			for(int w=q+1;w<r;w++) ans = min((a[s[q]]-a[s[w]]).dist(),ans);
		return ans;
	}
	int tl,tr,m=(l+r)/2;
	ans = min(min_dist(l,m),min_dist(m,r));
	for(tl=l;a[s[tl]].x<a[s[m]].x-ans && tl<m;tl++);
	for(tr=r-1;a[s[tr]].x>a[s[m]].x+ans && tr>=m;tr--);
	sort(s+tl,s+tr,cmpy);
	for(int q=tl;q<tr;q++)
		for(int w=q+1;w<min(tr,q+6);w++)
			ans = min(ans,(a[s[q]]-a[s[w]]).dist());
	sort(s+tl,s+tr,cmpx);
	return ans;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
#endif

	while(~sf("%d",&n),n) {
		if(n==0) break;
		rep(i,n) a[i].read();
		rep(i,n) s[i]=i;
		sort(s,s+n,cmpx);
//		dbg(n);
		D ans = min_dist(0,n);
		pf("%.2lf\n",ans/2.0);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值