Abner

博观而约取,厚积而薄发,不可择焉不精,语焉不详!

hdu 1007

点击打开资料

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iomanip>

using namespace std;

const int MAXN = 100100;

struct Point
{
	double x, y;
};

Point point[MAXN];
Point s[MAXN];
int cmp(Point a, Point b)
{
	return a.x < b.x;
}

int cmp1(Point a, Point b)
{
	return a.y < b.y;
}

double Calculate(Point a, Point b)
{
	return sqrt( (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) );
}


double Binary_search(int a, int b)
{
	int i, j;
	if(b - a == 1)
		return Calculate(point[a], point[b]);
	if(b - a == 2)
		return min( Calculate(point[a], point[a+1]), min(Calculate(point[a], point[b]), Calculate(point[a+1], point[b])) );
	int mid = (a + b) / 2;
	double m = min(Binary_search(a, mid), Binary_search(mid+1, b));
	for(i = mid-1; i >= a; --i)
	{
		if (point[mid].x - point[i].x >= m)
		{
			a = i;
			break;
		}
	}
	for (i = mid; i <= b; ++i)
	{
		if (point[i].x - point[mid-1].x >= m)
		{
			b = i;
			break;
		}
	}
	int cnt = 0;
	for (i = a; i <= b; ++i)
		s[cnt++] = point[i];
	sort(s, s+cnt, cmp1);
	for (i = 0; i < cnt; ++i)
	{
		for (j = i+1; j < cnt; ++j)
		{
			if(s[j].y - s[i].y > m)//在中位线上的投影小于m的值
				break;
			if(Calculate(s[i], s[j]) < m)
				m = Calculate(s[i], s[j]);
		}
	}
	return m;
}

int main()
{
	int n;
	while (cin>>n, n)
	{
		for (int i = 0; i < n; ++i)
			cin>>point[i].x>>point[i].y;
		sort(point, point+n, cmp);
		cout<<setiosflags(ios::fixed)<<setprecision(2)<<Binary_search(0, n-1)/2<<endl;
	}
	return 0;
}


阅读更多
版权声明:本文为博主原创文章,转载需注明出处。 https://blog.csdn.net/Hearthougan/article/details/17162311
文章标签: 计算几何
个人分类: 计算几何
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭