ZOJ 2107


平面最近点对问题。。  

这题以前看过一篇文章。貌似只用比较最近的6个点左右(X轴排序过的)就可以了。。

 A        B       C       G


D       E        F        H

假设这8个点在一个矩形上。。可以知道平面最近点对。。就是X轴周围的12个点中。。 O(n)算法就能过掉了。。


#include <queue>  
#include <stack>  
#include <math.h>  
#include <stdio.h>  
#include <stdlib.h>  
#include <iostream>  
#include <limits.h>  
#include <string.h>  
#include <algorithm>  
using namespace std;  
const int MAX = 100010;  
const double D_MAX = 1e50;  
const double eps = 1e-15;  
struct point{  
    double x,y;  
};  
point p[MAX];
bool xy(double x,double y) // x < y   
{  
    return x < y - eps;  
}  
bool dd(double x,double y)  // x == y   
{  
    return fabs( x - y ) < eps;  
}  
bool cmp(point a,point b)  
{  
    if( dd(a.x,b.x) )  
        return xy(a.y,b.y);  
    return xy(a.x,b.x);  
}  
double disp(point a,point b)  
{  
    return sqrt( (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y) );  
}  
double nearpot(int l,int r)  
{  
	double ans = D_MAX;
	for(int i = l;i <= r;i++){
		for(int j = 1; i+j <= r && j <= 6;j++){
			ans = min(ans,disp(p[i],p[j+i]));
		}
	}
	return ans;
}
int main(){
	int n;  
    while( ~scanf("%d",&n) && n )  
    {  
        for(int i=0; i<n; i++)  
            scanf("%lf %lf",&p[i].x,&p[i].y);     
        sort(p,p+n,cmp);  
        double ans = nearpot(0,n-1);  
        printf("%.2lf\n",ans/2.0);  
    }  
	return 0;  
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值