hud 1007 Quoit Design 求解平面最近点对的问题

HDOJ:1007---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 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

所谓最近点对呢,就是平面上给你一堆点,然后求出这堆点中相距最小的距离。

假如数据量比较小的话,那肯定是枚举所有点对求出各自距离再比较更方便了。很明显,枚举的时间复杂度是O(n²)。

不过一般不会给你这样的问题,点的个数往往很多,那就必然不能用枚举了。

这里要使用一种O(nlog(n))的算法。

这个算法主要思想就是分治,

算法描述:已知一个点的集合S,将S拆分成左右两部分求最近点对。首先对点集S进行排序,一般对点的X坐标从小到大排。算法每次选取一条直线(垂线)L,将S拆分成左右两部分Sl,Sr。L一般取点集S中所有点的中间点的x坐标来划分,这样可以保证SL和SR中的点数目各为n/2,否则,选取其他直线L划分可能导致Sl中有1个点,Sr中有n-1个点,这样算法的效率就很难保证了。依次找到Sl,Sr中的最短距离dl,dr。记录最短距离d=min(dl,dr);

如图:
这里写图片描述
很明显,只计算出Sl,Sr中的最短距离是不够的,S的最近点对很可能分别存在于Sl,Sr中。这种情况就要特殊处理了。

当然,只有在上图中两虚线之间的点才有再计算的意义,因为在这两条线之外的点与另一方的点的距离必然大于d。

对于Sl虚框范围内的某点P,在Sr虚框中与P点距离小于d的点顶多只有六个,就是图二右图中的2个正方形的6的顶点。

反证:假设Sr中存在一点Q与P的距离小于d,那么它与这个正方形的顶点的距离必然小于d,这与之前Sl,Sr中最短距离为d矛盾。

因此,对于Sl中的点P,不需要求出它与Sr中所有点的距离进行比较,根据鸽巢原理,只需要比较与P点Y坐标距离最近的6个点即可。这样大大降低了比较的次数。

求出Sl,Sr交界处的最短距离后对d进行更新即可。
这里写图片描述

算法时间复杂度: 首先对点集S的点x坐标和y坐标进行升序排序(快排),需要2nlogn次,

复杂度为O(2nlogn)在分治过程中,对于每个S’l中的点,都需要与S’r中的6个点进行比较,

复杂度为 O(n)。所以总的时间复杂度为O(nlogn),比起暴力枚举要好很多。

代码如下

//HDU 1007,代码模仿某位大神写的...Orz
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;
#define  MAXX 1<<30

struct point
{
    double x,y;
};
point p[100010];
int t[1000010];
double min(double a,double b)
{
    return a>b?b:a;
}

int cmp(const point &a,const point &b)         //对于点的x坐标排序;
{
    if(a.x==b.x) return a.y<b.y;
    return a.x>b.x;
}

int cmpy(const int &a,const int &b)    //对点的y坐标排序;
{
    return p[a].y<p[b].y;
}

double dis(point a,point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

double findClosepari(int left,int right)                //分治,递归。两部分中间的情况要特殊处理;
{
    double x=MAXX;
    if(left==right) return x;
    if(left==right-1) return dis(p[left],p[right]);
    int mid=(left+right)>>1;                      //求出中间点;
    double dl=findClosepari(left,mid);
    double dr=findClosepari(mid+1,right);
    dl=min(dl,dr);

    int i,j,k;                                 //下面把中间2dl宽度的部分的点单独处理;
    k=0;
    for(i=left;i<=right;i++)
        if(fabs(p[i].x-p[i+1].x)<dl) t[k++]=i;

    sort(t,t+k,cmpy);   //由下到上排序,扫描;
    for(i=0; i<k; i++)
        for(j=i+1; j<k&&p[t[j]].y-p[t[i]].y<dl; j++)
        {
            double d3 = dis(p[t[i]],p[t[j]]);
            dl=min(dl,d3);
        }
    return dl;
}


int main()
{
    int n;
    while(cin>>n,n)
    {
        for(int i=1;i<=n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        sort(p+1,p+n-1,cmp);
        double r=findClosepari(1,n)/2.0;
        printf("%.2lf\n",r);
    }
    return 0;
}
Quoit Design 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. 立即下载

相关推荐

风储VSG-基于虚拟同步发电机的风储并网系统Simulink仿真

内容概要:本文介绍了“Plecs全桥LLC谐振变换器仿真模型”这一技术资源,聚焦于隔离型全桥LLC谐振变换器的建模、仿真与参数设计,涵盖其在电力电子系统中的关键应用,如软启动、闭环控制、变频控制及效率优化等。该模型支持对LLC变换器在不同工作模式下的动态响应与性能表现进行深入研究,适用于探究谐振拓扑结构、磁性元件设计、谐振参数匹配以及先进控制策略的科研与工程实践。同时,文档配套提供了仿真模型、设计报告与计算书等完整资料,强化了其实用性与教学价值。; 适合人群:具备电力电子、自动化或电气工程等相关专业背景,从事电力电子变换器研发、系统仿真与控制算法设计的科研人员、工程师及研究生。; 使用场景及目标:①用于全桥LLC谐振变换器的建模、仿真与动态性能分析;②支持对软启动过程、闭环稳压控制、变频调谐及效率优化等核心技术的研究与教学;③为电力电子电源系统的工程设计、参数优化与控制策略验证提供高精度仿真平台。; 阅读建议:建议结合Plecs或Simulink等仿真工具进行动手实践,重点理解电路拓扑构建、谐振参数计算、控制器设计与系统稳定性分析,并配合文档中的设计报告与计算书,系统掌握从理论到仿真的完整流程。

套圈·分治

套圈题目信息测试样例解答 题目信息 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 desig

zhj12399的博客 1584

逆向工程师的Shopee爬虫避坑指南:af-ac-enc-dat参数生成原理与反反爬策略

本文深入剖析了Shopee电商平台的反爬虫加密机制,重点解析了核心请求头参数x-sap-ri、x-sap-sec及af-ac-enc-dat的生成原理与联动关系。文章从逆向工程视角,提供了定位加密代码、分析算法逻辑的实战方法,并分享了基于JS执行环境模拟与无头浏览器控制的可持续反反爬策略,为高效、稳定地采集数据提供专业指南。

weixin_28607671的博客 1495

c语言 最近点对问题(分治法+递归)

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

计算机科学与技术专业学生的成长之路 1万+

STM32电机库(ST-MC-Workbench)实战指南——三电阻电流采样电路参数配置详解

本文详细解析了STM32电机库(ST-MC-Workbench)中三电阻电流采样电路的参数配置方法,涵盖电阻选型、放大增益计算、滤波时间优化等关键步骤。通过实战案例和调试技巧,帮助工程师快速掌握电流采样技术,提升电机控制系统的精度与稳定性。特别针对ST-MC-Workbench界面参数设置提供了详细指导。

weixin_29186249的博客 505

基于STM32F103C8T6的红外循迹避障小车

编程思路:控制核心板IO口的电平(即电机驱动模块的AIN1,AIN2,BIN1,BIN2)从而控制AO1、AO2、BO1、BO2的电平高低,进而控制电机使其正转、反转、不转。由于单纯地拉高拉低电平只能让电机在转与不转的状态切换,故决定利用TIM4的两个通道分别产生两路PWM信号来控制电机速度,四个电机两个一组,同一侧的两个连接在同一个PWM上。(#include "dianji.h"文件中同时写了对循迹模块引脚的初始化)

weixin_48743503的博客 1万+

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

Acer12138的博客 1008

MIPI CSI-2数据加扰技术解析:LFSR实现与多通道同步策略

本文深入解析了MIPI CSI-2协议中的数据加扰技术,该技术通过线性反馈移位寄存器(LFSR)对图像数据进行随机化处理,旨在有效降低高速传输中的电磁干扰。文章详细阐述了LFSR的工作原理、D-PHY与C-PHY物理层不同的初始化与同步策略,并探讨了多通道同步的实现方法,为高速接口的EMC设计与调试提供了实践指导。

weixin_29207149的博客 386

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

qq_41886231的博客 511

深入解析数据通信中的码元速率与比特率:从基础到实战应用

本文深入解析了数据通信中的核心概念码元速率与比特率,通过生动的比喻和公式(比特率=波特率×log₂(M))揭示其本质关系。文章结合BPSK、QPSK、16QAM等调制技术,剖析了从4G/5G到LoRa等实战场景中如何权衡速率、距离与可靠性,并提供了软考真题解析与系统设计建议,帮助读者从理论到应用全面掌握数据速率优化。

weixin_29324877的博客 213

告别Windows!Oculus Quest 2在Mac M1上的串流实战(scrcpy+SideQuest避坑指南)

本文为Mac M1用户提供了Oculus Quest 2在macOS上实现高清低延迟串流的实战指南。通过使用开源工具scrcpy和SideQuest,绕开官方限制,详细讲解了从环境准备、无线调试到画质与延迟优化的完整步骤,重点解决了Apple Silicon芯片Mac的连接与性能问题,助你告别Windows依赖。

weixin_29224309的博客 308

hud 1007 Quoit Design(分治求最小点对)

hud 1007 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

这儿有个bug 394

微磁模拟实战指南:从OOMMF安装到首个3D模型运行

本文详细介绍了OOMMF微磁模拟软件的安装与使用,从环境配置到首个3D模型运行的完整教程。涵盖Windows、Linux和macOS平台的安装避坑指南,以及MIF文件编写、并行计算优化等实战技巧,帮助研究人员快速掌握这一开源工具在磁畴壁运动、磁滞回线等微磁学问题中的应用。

weixin_29294597的博客 153

15张图详解三方支付:总架构、5大核心业务处理流程、备付金管理

用户可以将银行卡与零钱账户绑定,建立银行结算账户与支付账户的连接,然后将银行账户资金充值到零钱账户中,或将零钱账号中的余额提现至绑定的银行卡。因此,用户在A支付机构的零钱一般只能支付给A支付机构的商户,如支付宝的零钱不能支付给微信的商户。这五大业务的协同运作,使得第三方支付机构这一庞大的支付服务平台能够为市场提供卓越的支付能力。到了结算时间,网联会向备付金集中存管账户提交结算指令,在第三方支付机构备付金集中存管账户内完成最终的结算,并将结算结果及额度变动情况通知给第三方支付机构,具体流程如图13所示。

银行信息系统应用架构详解 1044

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

最近点对问题

分而治之 题目来源: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 t

weixin_45776185的博客 363

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

翻译(5)

题目:Quoit Design | JXNUOJ 描述: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 care

Ling_1501的博客 548

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

分治学习日记(二)

例2 :套圈 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 ...

weixin_43869733的博客 345

HDU1007(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

weixin_43808717的博客 1675

Quoit Design - HDU 1007 分治

Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 30473    Accepted Submission(s): 8008 Problem Description Have you ev

提比的强行AC 855

算法复习——平面分治(hud1007)

题目: 问题描述 : 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 positi...

banzhangzhi8819的博客 159

【数据结构与算法设计-基础向】C/C++编程练习 - 套圈(分治法解决最近对问题)

【数据结构与算法设计-基础向】C/C++编程练习 - 套圈(分治法解决最近对问题)

qq_45487246的博客 1069

【HDU 1007】Quoit Design(分治法)

【HDU 1007】Quoit Design(分治法) 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 caref

Chen_yuazzy的博客 741

Quoit Design(分治加排序,最短点数对)

题目链接 这道题还是很有难度啊,最后借鉴了别人的题解才写出来????,总的来说这道题就是排序加二分,首先按照横坐标排序然后再对y坐标二分。 代码如下 #include<iostream> #include<algorithm> #include<math.h> #include<stdio.h> using namespace std; #define m...

qq_44722533的博客 375
上一篇: HDU 1284 钱币兑换问题
下一篇: 病毒侵袭 HDU - 2896
elbadaernu
博客等级 码龄10年 22粉丝 338原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值