HDU1007 Quoit Design 【分治】

Quoit Design

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 30505    Accepted Submission(s): 8017

Problem Description

Have 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.

Input

The 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.

Output

For 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

题意:给定n个点,求距离最短的两点的距离的一半。

题解:开始用暴力法,结果超时,然后换成分治就过了,分治的过程是先将每个点的坐标读入到数组里,再将数组按照x坐标排序,然后分治找最小值,递归终止条件是只剩两个元素或三个元素,但是若仅按照x排序最终结果不一定是最小值,因为有可能左边的元素与右边的元素构成最小值,所以需要再根据y值进行一次排序,此时数据规模已经相当小了,可以用暴力直接求解。

分治代码:

#include <stdio.h>
#include <math.h>
#include <algorithm>
#define maxn 100002
using std::sort;

struct Node{
	double x, y;
} arr[maxn], temp[maxn];

bool cmpx(Node a, Node b)
{
	return a.x < b.x;
}

bool cmpy(Node a, Node b)
{
	return a.y < b.y;
}

double calDist(int i, int j)
{
	double x = arr[i].x - arr[j].x;
	double y = arr[i].y - arr[j].y;
	return sqrt(x * x + y * y);
}

double divideAndConquer(int l, int r)
{
	if(r - l == 1) return calDist(l, r);
	else if(r - l == 2){
		double a = calDist(l, l + 1);
		double b = calDist(l + 1, r);
		double c = calDist(l, r);
		if(b > c) b = c;
		return a < b ? a : b;
	}
	int mid = (l + r) >> 1, i, j, id = 0;
	double a = divideAndConquer(l, mid);
	double b = divideAndConquer(mid + 1, r);
	double min = a < b ? a : b;
	for(i = l; i <= r; ++i)
		if(fabs(arr[i].x - arr[mid].x) < min) temp[id++] = arr[i];
	sort(temp, temp + id, cmpy);
	for(i = 0; i < id; ++i)
		for(j = i + 1; j < id; ++j){
			a = temp[j].y - temp[i].y;
			if(a >= min) break;
			b = temp[j].x - temp[i].x;
			a = sqrt(a * a + b * b);
			if(a < min) min = a;
		}
	return min;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	int n, i, j;
	double ans, x, y, len;
	while(scanf("%d", &n), n){
		for(i = 0; i < n; ++i)
			scanf("%lf%lf", &arr[i].x, &arr[i].y);
		sort(arr, arr + n, cmpx);
		printf("%.2lf\n", divideAndConquer(0, n - 1) / 2);
	}
	return 0;
}

原TLE代码:

#include <stdio.h>
#include <math.h>
#define maxn 100002

struct Node{
	double x, y;
} arr[maxn];

double cal(int i, int j)
{
	double x = arr[i].x - arr[j].x;
	double y = arr[i].y - arr[j].y;
	return sqrt(x * x + y * y);
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	int n, i, j;
	double ans, x, y, len;
	while(scanf("%d", &n), n){
		for(i = 0, ans = -1; i < n; ++i){
			scanf("%lf%lf", &arr[i].x, &arr[i].y);
			for(j = 0; j < i; ++j){
				len = cal(i, j);
				if(len < ans || ans < 0) ans = len;
			}
		}
		printf("%.2lf\n", ans / 2);
	}
	return 0;
}

HDU1007 Quoit Design 【分治】,布布扣,bubuko.com

时间: 2024-12-29 11:53:03

HDU1007 Quoit Design 【分治】的相关文章

hdu 1007 Quoit Design 分治求最近点对

Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29344    Accepted Submission(s): 7688 Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat

ZOJ - 2107 Quoit Design [分治]

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17751 题目描述:求最近点对 题目分析:分治,(nlogn): 为什么,第二轮按排序:http://noalgo.info/793.html 代码: //problem: zoj 2107 Quoit Design //author: ACsorry //result: Yes #include<iostream> #include<cstdio>

HDU 1007 [Quoit Design] 分治

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 题目大意:N个点,求最近一对点的距离的一半. 关键思想:最傻瓜式的枚举所有点对的距离找最短 O(n^2),会TLE. 分治可以优化成O(nlogn).二分区间,考虑三种情形:点对的两点都在左区间.两点都在右区间.两点一左一右 前两种情况,可以递归地解出来,分别为d1,d2.第三种,可以依据min(d1,d2)收缩成一条带状区域(勾股定理显然). 然后对带中所有点进行处理,此时又可依据min(d

[HDU1007]Quoit Design

题目描述 Description Have 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

HDU 1007 Quoit Design (分治)

Quoit Design Problem Description Have 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 ri

Quoit Design(hdu1007)最近点对问题。模版哦!

Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30919 Accepted Submission(s): 8120 Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings

Quoit Design(最近点对+分治)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 42865    Accepted Submission(s): 11128 Problem Description Have you ever played

Quoit Design(hdu1007)

---恢复内容开始--- Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 47126    Accepted Submission(s): 12323 Problem Description Have you ever played quoit in a playground? Quoit is a game

HDU 1007 Quoit Design【计算几何/分治/最近点对】

Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 58566    Accepted Submission(s): 15511 Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat