HDU1007 Quoit Design 【分治】

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115649.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 部署自己的GitLab

    部署自己的GitLab

    2022年3月6日
    40
  • 归一化函数normalize详解_求归一化常数A

    归一化函数normalize详解_求归一化常数A1.归一化归一化就是要把需要处理的数据经过处理后(通过某种算法)限制在你需要的一定范围内。首先归一化是为了后面数据处理的方便,其次是保证程序运行时收敛加快。归一化的具体作用是归纳统一样本的统计分布性。归一化在0-1之间是统计的概率分布,归一化在某个区间上是统计的坐标分布。归一化有同一、统一和合一的意思。归一化的目的,是使得没有可比性的数据变得具有可比性,同时又保持相比较的两个数据之间的相对关系,……

    2022年10月11日
    2
  • Maven–如何下载JSONObject相关依赖架包

    Maven–如何下载JSONObject相关依赖架包一、开发场景Java开发当中经常需要Json格式的数据,这就用到JSONObject类,本文章只提供以下两种JSONObject对应架包的下载方式。com.alibaba.fastjson.JSONObject(依赖1个架包fastjson-1.2.28.jar)net.sf.json.JSONObject(依赖6个架包commons-beanutils-1.9.3.jar、commons-c…

    2022年7月12日
    66
  • 自己动手写操作系统pdf_写作系统

    自己动手写操作系统pdf_写作系统2019-4-26AM9:15前言:记得上初中时,在一张英语报上看到一篇关于史蒂夫乔布斯的文章,那时他才20多岁,就已经达到人生的巅峰,可谓意气风发,我的内心对其充满崇敬之意。联想到表哥家的那台windows95大块头电脑,时常偷偷玩上两把魔兽争霸,那时,已经对这个魔术般奇幻的机器充满好奇。再后来一直到大学,在偌大的图书馆看到关于计算机的书籍,里边总是浮现一些不明所以的代码,既感到神奇的同…

    2022年10月20日
    2
  • Android-json解析(三):原生JSONObject+JSONArray的解析、遍历及生成等

    Android-json解析(三):原生JSONObject+JSONArray的解析、遍历及生成等一、JSONObject和JSONArray的数据表示形式JSONObject的数据是用{}来表示的,例如:{&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;id&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;:&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;123&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;,

    2022年7月13日
    17
  • 使用JAX-WS进行应用程序身份验证「建议收藏」

    使用JAX-WS进行应用程序身份验证「建议收藏」在JAX-WS中处理身份验证的常用方法之一是客户端提供“用户名”和“密码”,将其附加在SOAP请求标头中并发送到服务器,服务器解析SOAP文档并检索提供的“用户名”和“密码”从请求标头中进行,并从数据库中进行验证,或者使用其他任何方法。在本文中,我们向您展示如何实现上述“JAX-WS中的应用程序级别认证”。想法…在Web服务客户端站点上,只需将“用户名”和“密码”放入请…

    2022年7月15日
    22

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号