HDOJ 1007-Quoit Design

HDOJ 1007-Quoit Design

Quoit Design

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 77740    Accepted Submission(s): 20729

Problem DescriptionHave you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded.
In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a configuration of the field, you are supposed to find the radius of such a ring.

Assume that all the toys are points on a plane. A point is encircled by the ring if the distance between the point and the center of the ring is strictly less than the radius of the ring. If two toys are placed at the same point, the radius of the ring is considered to be 0.

InputThe input consists of several test cases. For each case, the first line contains an integer N (2 <= N <= 100,000), the total number of toys in the field. Then N lines follow, each contains a pair of (x, y) which are the coordinates of a toy. The input is terminated by N = 0.

OutputFor each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places.

Sample Input

2
0 0
1 1
2
1 1
1 1
3
-1.5 0
0 0
0 1.5
0

Sample Output

0.71
0.00
0.75

AuthorCHEN, Yue 
SourceZJCPC2004
RecommendJGShining   |   We have carefully selected several similar problems for you:  10061009100510081004


给定二维平面上的N个点,问其中距离最近的两个点的距离是多少,输出最短距离的一半。

典型的平面上最近点对问题。大学算法课上有,采用分治法。即首先把所有点按x排序,然后取x轴中点m,把所有点划分为左右两半L和R,在L和R中递归求最近点对的距离,假设为dL和dR。假设d=min(dL,dR)。则还需要判断最近点对是否会在中点m附近,假设存在这种情况,则跨越m的最近点对必须满足距离m小于d。把这部分点收集起来,按y排序,然后求任意两点之间的距离,期间可以break加速。

完整代码如下:

#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;

struct Point
{
	double x_, y_;
	Point() :x_(0), y_(0) {};
	Point(double x, double y) :x_(x), y_(y) {};
};

bool CmpX(const Point &p1, const Point &p2) {
	return p1.x_ < p2.x_;
}

bool CmpY(const Point &p1, const Point &p2) {
	return p1.y_ < p2.y_;
}

// 暂时只算距离平方,最后开平方根
double CalDist(const Point &p1, const Point &p2) {
	return (p1.x_ - p2.x_)*(p1.x_ - p2.x_) + (p1.y_ - p2.y_)*(p1.y_ - p2.y_);
}

double CalMinDist(vector<Point> &data, int l, int r) {

	if (r - l == 2) {
		return CalDist(data[l], data[l + 1]);
	}
	else if (r - l == 3) {
		double d1 = CalDist(data[l], data[l + 1]);
		double d2 = CalDist(data[l], data[l + 2]);
		double d3 = CalDist(data[l + 1], data[l + 2]);
		return min(min(d1, d2), d3);
	}

	int m = l + (r - l) / 2;
	int mx = data[m].x_;
	double dd = min(CalMinDist(data, l, m), CalMinDist(data, m, r));
	double d = sqrt(dd);

	vector<Point> rect;
	for (int i = l; i < r; ++i) {
		if (data[i].x_ > mx - d && data[i].x_ < mx + d) {
			rect.push_back(data[i]);
		}
	}

	sort(rect.begin(), rect.end(), CmpY);
	for (int i = 0; i < rect.size(); ++i) {
		for (int j = i + 1; j < rect.size(); ++j) {
			double tmpd = CalDist(rect[i], rect[j]);
			if (tmpd > dd)break;
			dd = min(dd, tmpd);
		}
	}

	return dd;
}

int main() {
	freopen("input.txt", "r", stdin);

	int n;
	while (scanf("%d", &n)) {
		if (n == 0)break;
		vector<Point> data(n, Point());
		for (int i = 0; i < n; ++i) {
			scanf("%lf %lf", &data[i].x_, &data[i].y_);
		}
		sort(data.begin(), data.end(), CmpX);
		printf("%.2lf\n", sqrt(CalMinDist(data, 0, data.size())) / 2);
	}

	return 0;
}

本代码提交AC,用时982MS。

参考: https://www.cnblogs.com/MartinLwx/p/9757828.htmlhttps://www.jianshu.com/p/8bc681afbaff

Leave a Reply

Your email address will not be published. Required fields are marked *