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


相关推荐

  • 又拍云黄慧攀QCon 2016技术分享:直播平台架构与实施

    又拍云黄慧攀QCon 2016技术分享:直播平台架构与实施

    2021年9月14日
    56
  • telnet安装_cmd安装telnet命令

    telnet安装_cmd安装telnet命令telnet安装

    2025年8月5日
    6
  • GateWay 网关跨域问题「建议收藏」

    GateWay 网关跨域问题「建议收藏」yml文件中配置即可:spring:cloud:gateway:globalcors:#全局的跨域处理add-to-simple-url-handler-mapping:true#解决options请求被拦截问题corsConfigurations:'[/**]’:allowedOrigins:#允许哪些网站的跨域请求allowedOrigins:“*”允许所有网站…

    2022年10月9日
    3
  • Java详解:淘宝秒杀脚本java

    Java详解:淘宝秒杀脚本java造成雪崩的真实场景1.4.1服务提供者不可用硬件故障:如网络故障、硬盘损坏等。程序的bug:如算法需要占用大量CPU的计算时间导致CPU使用率过高。缓存击穿:比如应用刚重启,短时间内缓存是失效的,导致大量请求直接访问到了数据库,数据库不堪重负,服务不可用。秒杀和大促:服务短时间承载不了那么多请求量。1.4.2重试加大流量用户连续重试:比如用户看到界面上没有响应,所以又操作了一遍,结果又增加了一倍请求量。程序重试机制:比如代码中有多次重试的逻辑,一次失

    2022年6月1日
    37
  • 900万!!!!!!!!这也太强了吧!!!我的老天!!!!!!!!!!

    900万!!!!!!!!这也太强了吧!!!我的老天!!!!!!!!!!大家好,我是二哥呀!之前在送书的时候做了一个小调查,问题是:“你是怎么认识二哥的?”我以为从知乎上了解的多一些,没想到,CSDN上的最多,看来二哥还是在CSDN上更有影响力一些,这个结果多少让我感到有些意外,因为我最近在知乎上更新得更勤快一些。写这篇文章的时候,我去CSDN上看了一眼我的主页。访问量突破了900万!按照目前的增长速度来看,年底突破1000万访问量应该没啥大问题。另外还有一些数据我觉得也挺牛逼的:原创文章数量957篇;作者总榜第12名;作者周榜第

    2022年6月7日
    30
  • Java多线程详解_java支持多线程

    Java多线程详解_java支持多线程一、线程生命周期一个线程被实例化完成,到线程销毁的中间过程1.新生态:New一个线程对象被实例化完成,但是没有做任何操作2.就绪态度:Ready一个线程被开启,并且开始抢占CPU时间3.运

    2022年8月16日
    7

发表回复

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

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