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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java volatile作用

    Java volatile作用javavolatile作用

    2022年7月18日
    15
  • 如何通过maven打包可执行jar包[通俗易懂]

    如何通过maven打包可执行jar包[通俗易懂]一、目的将代码打包成jar包有四种形式:1、只打包本项目的代码,不包括依赖的jar包,并且不可直接通过java-jarxxx.jar执行(应用场景:我们日常使用依赖的jar包)2、只打包本项目的代码,不包括依赖的jar包,并且可以直接通过java-jarxxx.jar执行(应用场景:执行时依赖的jar包存在在本jar包外部,减少jar体积)3、打包本项目的代码,同时将依赖的jar包解压后的文件复制到本jar包中,可以直接通过java-jarxxx.jar执行(应用场景:直接执行,

    2022年10月4日
    2
  • panda’_pandas map

    panda’_pandas mappandas.DataFrame.iloc()纯基于位置的整数索引输入格式:一个整数列表或数组,如[4,3,0]。一个带有int类型的slice对象,例如1:7。一个布尔值数组。一个具有一个参数的可调用函数,返回索引案例mydict=[{‘a’:1,’b’:2,’c’:3,’d’:4},{‘a’:100,’b’:200,’c’:300,’d’:400},{‘a’:1000,’b’:2000,’c’:30

    2022年8月30日
    1
  • C++编程技巧—对数运算实现

    C++编程技巧—对数运算实现可以调用 C C 中现成的算法库实现整数对数运算 比较高效的 64 位整数对数运算实现方法如下 intLog2 uint64 tn intresult if n amp 0xffffffff00 result 32 n 32 if n amp 0x00000000ff

    2025年8月27日
    3
  • 虚拟机VMware下载与安装教程(详细)

    虚拟机VMware下载与安装教程(详细)文章目录虚拟机VMware的下载虚拟机VMware的安装虚拟机VMware的下载虚拟机VMware的安装1.虚拟机VMware的下载官网地址:https://www.vmware.com/cn.html以下为官网界面选择“产品”—>“个人桌面”—>“WorkstationPro”选择“下载”(这里虽然是“试用Workstation15.5Pro”,但是点击“下载”之后,将会跳转到“Workstation16Pro”的下载界面)根据自己电脑的系统,比如你的电脑是Wi

    2022年6月7日
    39
  • 第七届蓝桥杯(软件类)C++决赛A组题解

    第七届蓝桥杯(软件类)C++决赛A组题解文章目录题目链接A组真题题目结构第一题随意组合第二题拼棋盘第三题打靶第四题路径之谜第五题碱基第六题圆圈舞(待补)题目链接A组真题题目结构题目类型第一题随意组合结果填空第二题拼棋盘结果填空第三题打靶代码填空第四题路径之谜程序设计第五题碱基程序设计第六题圆圈舞程序设计第一题随意组合问题重现小明被绑架到X星球的巫师W那里。其时,W正在玩弄两组数据(2358)和(1467

    2022年7月24日
    8

发表回复

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

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