hdu 1007

原创 2013年12月06日 10:20:23

点击打开资料

#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;
}


版权声明:本文为博主原创文章,转载需注明出处。

hdu1007 Quoit Design 平面最近点对(分治)

题意:就是平面最近点对←_←思路:先把点按x坐标排好序,利用分治的思想,比如要求区间[l, r]的最近点对,先求区间[l, mid]的最近点对,再求[mid + 1, r]的最近点对,设此时的最近距离...

HDU1007——Quoit Design(最近点问题)

Quoit Design 今天从这里开始^_^ Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java...
  • curson_
  • curson_
  • 2016年08月06日 11:10
  • 185

hdu 1007最接近点对问题 nlogn复杂度

#include #include #include #include using namespace std; #define INF 100000000 int n; struct poi...

hdu1007求最近点对(分治法)

#include #include #include #include #include #include #include #include #include #include #include #...

hdu 1007 Quoit Design 最近点对(分治)

代码#include #include #include #include #define MAXN 100005 //#define min(a,b) (a

hdu 1007 Quoit Design(分治求最近点对)

Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To...

hdu计算机学院大学生程序设计竞赛(2015’11)1007 油菜花王国

油菜花王国 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su...

(hdu1007)Quoit Design,求最近点对

Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings...

HDU4893 2014多校第三场1007 Wow! Such Sequence!

线段树题目...花了一下午时候去做..提交一直WA,不知道错在哪,后来考虑到中间过程...

2016 暑假多校训练 第三场 1007 Explorer Bo HDU 5758

#pragma comment(linker,"/STACK:1024000000,1024000000") #include #include #include #include #incl...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 1007
举报原因:
原因补充:

(最多只允许输入30个字)