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


相关推荐

  • vr体验心得_在我们新的VR学习体验中逃脱女巫的小屋

    vr体验心得_在我们新的VR学习体验中逃脱女巫的小屋vr体验心得OurbrandnewprojectforUnityLearnisanimmersiveVRescaperoom.ExplorethepotentialofVRinUnityandcreateyourownexperienceinasimpleprototypeenvironment.Letusintroduceyou…

    2022年10月1日
    0
  • OWASP TOP10系列之#TOP1# A1-注入类「建议收藏」

    OWASP TOP10系列之#TOP1# A1-注入类「建议收藏」OWASPTOP10系列之#TOP1#注入类提示:本系列将介绍OWASPTOP10安全漏洞相关介绍,主要针对漏洞类型、攻击原理以及如何防御进行简单讲解;如有错误,还请大佬指出,定会及时改正~文章目录OWASPTOP10系列之#TOP1#注入类前言一、注入类漏洞是什么?二、什么情况下会产生注入类漏洞问题?三、如何预防?四、具体示例1.SQL注入2.OS命令注入3.XPath注入总结前言在OWASP(开放式Web应用程序安全项目)公布的10项最严重的Web应用程序安全风险列表的在

    2022年5月15日
    34
  • Vue生成二维码_vue通过二维码分享

    Vue生成二维码_vue通过二维码分享转存vue生成二维码并下载1、下载插件npminstall–saveqrcodejs22、引入constQRCode=require(“qrcodejs2″)3、组件使用<template><divclass=”qr_code”><divstyle=”display:flex;align-items:center”>地址:<Inputid=”text”type=”te

    2022年9月28日
    0
  • 【深度学习】5:CNN卷积神经网络原理

    【深度学习】5:CNN卷积神经网络原理前言:先坦白的说,深度神经网络的学习在一开始对我造成的困扰还是很大的,我也是通过不断地看相关的视频资料、文献讲解尝试去理解记忆。毕竟这些内容大多都是不可查的,我们看到的都只是输入输出的东西,里面的内部运作以及工作原理,都需要沉心静思。这篇CNN卷积神经网络的原理介绍,也是自己通过收集来的资料阅读、理解、操练后,有了一定的见解后才拙笔,里面的内容我会尽量详尽,不清楚明白的地方,望大家慧眼指出。–—

    2022年7月20日
    11
  • DeepFake技术–DeepFakes 概述(一)(二)

    DeepFake技术–DeepFakes 概述(一)(二)AI换脸技术——DeepFakes概述(一)编者按:本文由图普科技编译自ExploringDeepFakes。2017年12月,一个名为“DeepFakes”的用户在Reddit上发布了一个“假视频”,视频中的艺人其实是后期加上的,但是看起来几乎毫无破绽。他利用了深度学习和AI新技术,在成人电影中把演员的脸替换成某个艺人的脸,从而制作成了这个看上去以假乱真的视频。从视频发布以后…

    2022年5月26日
    61
  • SQL数据库学习之路(一)

    SQL数据库学习之路(一)1.数据库简介(一个放数据的仓库)解决的问题:持久化存储,优化读写,保证数据的有效性关系型数据库:基于E-R模型(实体-联系图EntityRelationship)使用sq|语言进行操作(SQ

    2022年8月2日
    11

发表回复

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

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