杭电 ACM 1007 Quoit Design

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 i... 阅读详情

Quoit Design

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 52536    Accepted Submission(s): 13852


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
 

Author
CHEN, Yue
 

Source
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:   1006  1002  1001  1011  1024 

最近点对分治的问题



代码参考了一位博主的贴出的代码,在修改了他的代码bug后最终如下所示,(不改bug前也AC了(⊙﹏⊙)),谢谢那位博主提供的代码风格,也谢谢他在解答此题时推荐的书,但由于他博客地址找不到了,只能在此口头感谢了



AC(1)这个更耗时间和内存,原本想着new能灵活控制,反而忽略了频繁的new,其本身需要携带比预设一个数组要多的一些管理的信息,更加耗时与耗空间,(而new的时候一开始也忽略了delete,占的内存就更夸张了)
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 100007

using namespace std;

struct Point
{
    double x, y;
}*pt;
int *a;

int n;

bool cmp_x(Point a, Point b)
{
    if (a.x != b.x) return a.x < b.x;
    else return a.y < b.y;
}
bool cmp_y(int id1, int id2) {
    return pt[id1].y < pt[id2].y;
}
double getDis(const Point &a, const Point &b)
{
    double x = a.x - b.x;
    double y = a.y - b.y;
    return sqrt(x*x + y*y);
}
double solve(int l, int r)
{
    double ans = 0;
    if (r - l + 1 <= 3)
    {
        if (r - l + 1 == 1) return ans;
        ans = getDis(pt[l], pt[l + 1]);
        if (r - l + 1 == 2) return ans;
        for (int i = l; i < r; ++i)
        {
            for (int j = i + 1; j <= r; ++j)
            {
                ans = min(ans, getDis(pt[i], pt[j]));
            }
        }
        return ans;
    }
    int m = (l + r) >> 1;
    double s1 = solve(l, m);
    double s2 = solve(m + 1, r);
    ans = min(s1, s2);
    int k = 0;
    for (int i = m; i >= l && pt[m].x - pt[i].x <= ans; --i) 
        a[k++] = i;
    for (int i = m + 1; i <= r && pt[i].x - pt[m].x <= ans; ++i) 
        a[k++] = i;
    sort(a, a + k, cmp_y);
    for (int i = 0; i < k; ++i)
    {
        for (int j = i + 1; j < k && j <= i + 7; ++j)//此处只搜索点i后的七个点非常关键,不加这个限制会超时,而这个限制的根据由来贴在下面
        {
            ans = min(ans, getDis(pt[a[i]], pt[a[j]]));
        }
    }
    return ans;
}
int main()
{
    while (~scanf("%d", &n))
    {
        if (!n) break;
        pt = new Point[n];
        a = new int[n];
        for (int i = 0; i < n; ++i) scanf("%lf%lf", &pt[i].x, &pt[i].y);
        sort(pt, pt + n, cmp_x);
        printf("%.2lf\n", solve(0, n - 1) / 2.0);
        delete []pt;
        pt = NULL;
        delete []a;
        a = NULL;
    }
    return 0;
}
//AC(2)
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 100007

using namespace std;

struct Point
{
    double x,y;
}pt[N];
int a[N];

int n;

bool cmp(Point a, Point b)
{
    if (a.x != b.x) return a.x < b.x;
    else return a.y < b.y;
}
bool cmp_y(int id1, int id2){
    return pt[id1].y < pt[id2].y;
}
double getDis(const Point &a, const Point &b)
{
   double x = a.x - b.x;
   double y = a.y - b.y;
   return sqrt(x*x + y*y);
}
double solve(int l, int r)
{
    double ans = 0;
    if (r - l + 1 <= 3)
    {
        if (r - l + 1 == 1) return ans;
        ans = getDis(pt[l], pt[l + 1]);
        if (r - l + 1 == 2) return ans;
        for (int i = l; i < r; ++i)
        {
            for (int j = i + 1; j <= r; ++j)
            {
                ans = min(ans, getDis(pt[i],pt[j]));
            }
        }
        return ans;
    }
    int m = (l + r) >> 1;
    double s1 = solve(l, m);
    double s2 = solve(m + 1,  r);
    ans = min(s1,s2);
    int k = 0;
    for (int i = m; i >= l && pt[m].x - pt[i].x <= ans; --i) a[k++] = i;
    for (int i = m + 1; i <= r && pt[i].x - pt[m].x <= ans; ++i) a[k++] = i;
    sort(a, a + k, cmp_y);//按照y的大小顺序从小到大排序
    for (int i = 0; i < k; ++i)
    {
        for (int j = i + 1; j < k && j <= i + 7; ++j)
        {
            ans = min(ans, getDis(pt[a[i]], pt[a[j]]));//使用a[i]可以确保所对比的点就是两区域的中间点,a[x]其实就是第几个点,pt[a[x]]
							//是取这个点的详细信息,
        }
    }
    return ans;
}
int main()
{
    while (~scanf("%d",&n))
    {
        if (!n) break;
        for (int i = 0; i < n; ++i) scanf("%lf%lf",&pt[i].x, &pt[i].y);
        sort(pt, pt + n, cmp);
        printf("%.2lf\n",solve(0, n - 1)/2.0);
    }
    return 0;
}



 
 

 

 

杭电ACM第1007题——Quoit Design WA #include #include #include using namespace std; const int SIZE = 100005; typedef struct { double x; double y; }point; point p[SIZE], c[SIZE]/*用作辅助数组*/; double getDistance(point &a, point 阅读详情

相关推荐

『杭电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 pos...

漠宸离若的博客 404

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

杭电答案 1000-1007 全是C编写

杭电答案 1000-1007 全是C编写 安全用C编写,有各别的为C++编写

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 po

chenjun15的博客 798

分治法(算法竞赛,ACM程序设计)

ACM程序设计,分治法的课件,相关练习,以及各种题型,由简单到复杂,由容易到困难的各个阶段。是学习这一基本算法的很好的辅助资料。

acm竞赛分治算法总结

acm竞赛分治算法总结!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

杭电OJ 1007 Quoit Design

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

陈纪宏的博客 764

计算机毕业设计springboot股票模拟交易系统的设计与实现u6m959【附源码+数据库+部署+LW】

选题背景: 随着互联网和金融科技的快速发展,股票市场成为了吸引人们关注和参与的重要领域之一。越来越多的人开始对股票投资感兴趣,希望通过交易股票来获取收益。然而,对于初学者来说,股票市场的复杂性和风险往往让他们望而却步。因此,设计和实现一个股票模拟交易系统,可以帮助初学者更好地理解和掌握股票交易的规则和技巧,提高他们的投资决策能力。 意义: 股票模拟交易系统的设计与实现具有以下几个重要意义: 提供实践机会:股票模拟交易系统为投资者提供了一个实践的平台,使他们能够在真实市场环境下进行模拟交易,了解股票市场的

gp12081的博客 1768

杭电ACM题1007

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

laymenISmouse的专栏 1685

钉钉开发(三)——Java推送钉钉待办任务

之前一篇博文中,讲解了如何推送钉钉群消息,感兴趣的可以点击查看。 钉钉开发(一)——Java给钉钉群推送消息

Monten_Cristo的博客 6418

十大排序算法详解(一)冒泡排序、选择排序、插入排序、快速排序、希尔排序

冒泡排序、选择排序、插入排序、快速排序、希尔排序

HK的博客 18万+

杭电ACM OJ 1007 Quoit Design 最近点对 分治 递归

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

qq_36523667的博客 727

cocos2dx lua + protobuf

vs 2015 cocos2dx 3.10  lua  protobuf1. 下载 protobuf 源码     https://github.com/bnnswyp/protobuf/tree/master/protobuf-2.5.0    解压后的文件列表如下所示         其中 vsprojects 文件夹为 protobuf 的vs 工程2. 打开 protobuf.sln 编译...

u014486706的博客 2169

HDU 1007:Quoit Design && ZOJ 2107:Quoit Design

传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1107

丧心病狂 943

Matlab GUIDE -- 串口操作

1、首先新建-->应用程序-->GUIDE。 2、绘制一个简单的UI。 3、然后右击“打开串口”图标-->查看回调-->callback,即可打开此图标的回调函数,此回调函数是press button时执行(可按需要选择其他回调)。 4、在回调函数中添加打开串口的代码。 delete(instrfindall); %删除所有串口 s3 = serial('CO

Asgard-X 2843

YOLOv11的多任务学习:检测+分割+姿态估计-(扩展模型头部分支,实现多任务联合训练)

YOLOv11作为Ultralytics团队于2024年9月发布的最新YOLO系列模型,在前代版本基础上进行了多项架构创新,使其成为支持多任务学习的理想框架。其核心改进包括引入C2PSA(跨阶段局部自注意)模块,将跨阶段局部网络和自注意机制优势结合,使模型能够更有效地跨多层捕获上下文信息。C3k2模块取代了之前的C2f模块,作为CSP Bottleneck的自定义实现,使用两个较小卷积核而非一个大卷积核,在保持准确性的同时提高了效率。 多任务学习(Multi-Task Learning, MTL)在YOL

本博客聚焦 YOLOv11 全流程落地,涵盖架构优化、数据集处理、训练技巧与多场景部署,兼及国产数据库、Java 开发与 AI 模型应用,内容兼顾理论与工程实践,为开发者提供系统干货,助力高效提升技术能力。 659

HDU 1007 Quoit Design竟然要分治

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

lccurious的专栏 813

Unity零基础到进阶 | Unity中的内存泄漏全解析,原因、排查及避免

Unity 内存泄漏并非传统C++语境中的野指针或未释放堆内存,而是基于其「托管内存(C#)+ 原生内存(引擎/非托管)」双架构的特殊问题——即已废弃、不再被业务逻辑使用的内存块,无法被引擎或系统自动回收,持续占用内存空间,最终引发内存暴涨、GC卡顿、OOM(内存溢出)崩溃,其中移动端、主机端受影响最显著。 Unity 内存主要分为三类,其中泄漏仅发生在前两类,栈内存因系统自动分配/释放,无泄漏风险:

努力前行,总会成为自己心中的那道光 1万+

分治算法五(最近点对---杭电OJ 1007 Quoit Design)

1、问题描述 杭电OJ 1007链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 即给定坐标系上N个点,找到距离最短的两个点。 2、思路解析 ----->如果直接利用两两点比较的话,复杂度太高,为O(n^2),会导致超时 ----->简化问题:考虑一维数轴上点的情况,如果对这些点排序O(nlgn),那么最后只需要用O(n)时间就可以找

JPing的专栏 1471

统计学习导论(ISLR) 第四章分类算法课后习题

统计学习导论(ISLR) 参考资料: The Elements of Statistical Learning An Introduction to Statistical Learning 统计学习导论(ISLR)(二):统计学习概述 统计学习导论(ISLR)(三):线性回归 统计学习导论(ISLR)(四):分类 ISLR统计学习导论之R语言应用(二):R语言基础 ISLR统计学习导论之R语言应用(三):线性回归R语言代码实战 ISLR统计学习导论之R语言应用(四):分类算法R语言代码实战 统计学习导论

阿里云专家博主、数据科学领域优质创作者、统计er在读 2万+

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

杭电OJ——1007 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...

weixin_33711641的博客 122

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

杭电ACM——1007最近点对问题

首先介绍下二分法查找,又叫对分查找:         给定一个整数X 和整数A0,A1,A2,.............An-1, 后者已经预先排序并在内存中,求使得Ai = X 的下标i,如果X不在数据中,则返回i = -1。       一个好的策略是验证X是否是居中的元素,如果是,则答案就找到了,如果X小于居中元素,那么我们可以用同样的策略于居中居中元素左边已经排序的子序列;同理,如果X

大冰的小屋 2003

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

分治法

经典:最大子段和问题: 在数组中找到连续数据的和最大: C++ int maxSum(int a[],int left, int right)   {       int sum = 0;       if(left == right)   //如果序列长度为1,直接求解       {           if(a[left] > 0) sum = a[l

xietingcandice的专栏 785

杭电OJ 1007(C++)

#include <iostream> #include <algorithm> #include <cmath> #include <iomanip> using namespace std; const int SIZE = 100005; const int L = -1; const int R = 1; struct coord //...

Intelligence1028的博客 493

2024杭电多校4——1007 序列更新

取个倒数才是我们需要的形式,指数是i+1,所以相当于从1开始,所以。让它用第一种方式去更新,因为小的数更容易被更新,对于。(第二种方式),因为大的数更容易更新别的数。大概是因为两种维护方法都与b的第。一种比较玄学的优化方式,长知识了。

theskyinblue的博客 655
上一篇: 杭电ACM 1010 Tempter of the Bone
SimonHu-real
博客等级 码龄10年 0粉丝 10原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值