HDU1007 求最短距离的点对

#include<iostream>
#include<cmath>
#include<algorithm>
#include<iomanip>
using namespace std;
struct sys
{
	double x;
	double y;
};

sys num[100001],num1[100001],num2[100001];

bool cmpy(sys dot1,sys dot2)
{
	if(dot1.y==dot2.y) return dot1.x<dot2.x;
	return dot1.y<dot2.y;
}

bool cmpx(sys dot1,sys dot2)
{
	if(dot1.x==dot2.x) return dot1.y<dot2.y;
	return dot1.x<dot2.x;
}

double getmin(double a,double b,double c)
{
	double minx=min(a,b);
	if(minx>c) minx=c;
	return minx;
}
double getdistancex(sys dot1,sys dot2)
{
	double m=sqrt((dot1.x-dot2.x)*(dot1.x-dot2.x)+(dot1.y-dot2.y)*(dot1.y-dot2.y));
	return m;
}
double getdistancey(int low,int high)
{
	int count = high -low;
	double dis=0;
	if(count==0)  return 0;
	else if(count==1) {
		dis=getdistancex(num[low],num[high]);
		return dis;
	}
	else if(count==2)
	{
		double tem1= getdistancex(num[low],num[low+1]);
		double tem2= getdistancex(num[low+1],num[high]);
		double tem3= getdistancex(num[low],num[high]);
		dis=getmin(tem1,tem2,tem3); 
		return dis;
	}
	else 
	{
		double leftmin,rightmin;
		int mid=(high + low)/2;
		
		leftmin=getdistancey(low,mid);
		rightmin=getdistancey(mid+1,high);
		dis=min(leftmin,rightmin);
		
		int p=0;
		for(int i=low;i<=high;i++)
		{
			if(fabs(num[i].x-num[mid].x)<dis)
			num1[p++]=num[i];
		}
		sort(num1,num1+p,cmpy);
		mid=p/2;
		int k=0;
		for(int i=0;i<p;i++)
		{
			if(fabs(num1[i].y-num1[mid].y)<dis)
			num2[k++]=num1[i];
		}
		for(int i=0;i<k;i++)
		{
			for(int j=i+1;j<k;j++)
			{
				if(getdistancex(num2[i],num2[j])<dis)
				dis=getdistancex(num2[i],num2[j]);
				
			}
		}
		return dis;
	}
}
int main()
{
	int n;
	while(cin>>n&&n!=0)
	{
		for(int i=0;i<n;i++)
		{
			cin>>num[i].x>>num[i].y;
		}
		sort(num,num+n,cmpx);
		double min=getdistancey(0,n-1);
		cout<<fixed<<setprecision(2)<<min/2<<endl;
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值