hdu1007
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.
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.
ring required by the Cyberground manager, accurate up to 2 decimal places.
0
首先,假设点是n个,编号为1到n。我们要分治求,则找一个中间的编号mid,先求出1到mid点的最近距离设为d1,还有mid+1到n的最近距离设为d2。这里的点需要按x坐标的顺序排好,并且假设这些点中,没有2点在同一个位置。(若有,则直接最小距离为0了)。
然后,令d为d1, d2中较小的那个点。如果说最近点对中的两点都在1-mid集合中,或者mid+1到n集合中,则d就是最小距离了。但是还有可能的是最近点对中的两点分属这两个集合,所以我们必须先检测一下这种情况是否会存在,若存在,则把这个最近点对的距离记录下来,去更新d。这样我们就可以得道最小的距离d了。
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
struct point
{
double x,y;
}; int n,flag[];
point p[]; bool cmpx(point a,point b)
{
return a.x<b.x;
} bool cmpy(int a,int b)
{
return p[a].y <p[b].y;
} double bijiao(double a,double b)
{
return a<b?a:b;
} double juli(point a,point b)
{ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} double close(int l,int r)
{
double ans;
if(l+==r)
return juli(p[l],p[r]);
if(l+==r)
return bijiao(bijiao(juli(p[l],p[r]),juli(p[l+],p[r])),juli(p[l],p[l+]));
int mid=(r+l)/;
ans=bijiao(close(mid+,r),close(l,mid));
int cnt=;
for(int i=l;i<=r;i++)
{
if(p[i].x<=p[mid].x+ans&&p[i].x>=p[mid].x-ans)
flag[cnt++]=i;
}
sort(flag,flag+cnt,cmpy);
for(int i=; i<cnt; i++)
{
for(int j=i+; j<cnt; j++)
{
if(p[flag[j]].y-p[flag[i]].y>=ans)
break;
ans=bijiao(juli(p[flag[i]],p[flag[j]]),ans);
}
}
return ans;
} int main()
{
while(scanf("%d",&n)&&n)
{
for(int i=; i<n; i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
sort(p,p+n,cmpx);
printf("%.2lf\n",close(,n-)/);
}
return ;
}
hdu1007的更多相关文章
- 【代码笔记】iOS-标题2个图标,点击的时候,页面跳转
一,效果图. 二,工程图. 三,代码 RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
- HDU1007最近点对(分治)
http://acm.hdu.edu.cn/showproblem.php?pid=1007 直接见代码吧.不过这个是N*logN*logN的 尽管如此,我怎么感觉我的比他们的还快??? #inclu ...
- HDU-1007 Quoit Design 平面最近点对
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 简单裸题,测测模板,G++速度快了不少,应该是编译的时候对比C++优化了不少.. //STATU ...
- HDU1007 Quoit Design 【分治】
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- ACM-计算几何之Quoit Design——hdu1007 zoj2107
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDU1007.Quoit Design
-- 点我 -- 题目大意 :给你一堆点,求一个最小圆能够覆盖两个点的半径(最近两点距离的一半): 最多100000个点,暴力即O(n^2)会超时,考虑二分,先求左边最短距离dl,右边dr, 和一个点 ...
- 最近点对HDU1007
利用二分的方法来计算,应该是说利用分治的方法吧! 刚开始感觉时间会爆 后来发现嘎嘎居然没有 ,嗨自己算错了时间: #include <iostream> #include<cstdi ...
- Quoit Design(hdu1007)最近点对问题。模版哦!
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU1007(最近点对)
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
随机推荐
- 队列理论和队列网络模型 queueing theory and queueing network model
1队列理论 1.1队列在生活中随处可见,例如排队买票,排队打饭,排队做地铁等等.那将诸如此类的队列抽象一下,可归纳为一下5要术: 到达过程arrival process 服务时间的分布 service ...
- Begin using git
First thing first, you can easily install git in all 3 mainstream OS, Windows, Linux, OSX. Get windo ...
- 【测试】使用hr用户下的employees和departments表写一条SQL语句,(MG连接)
SQL> select * from employees d, departments t where d.department_id=t.department_id; rows selecte ...
- 【MySQL】MySQL事务回滚脚本
MySQL自己的 mysqlbinlog | mysql 回滚不好用,自己写个简单脚本试试: 想法是用mysqlbinlog把需要回滚的事务区域从mysql-bin.file中找到,然后通过脚本再插入 ...
- 学习STL-介绍一下STL
从大学时就开始学习C++,到现在近5年的时间了却很少用到STL.现在想想真得是对不起这门语言,也对不起宝贵的五年光阴.我钟爱C++,所以一定要完全搞懂它,理解它.爱一个人的前提是要懂他(她),爱一门语 ...
- 获取本地IP地址信息
2012-06-05 /// <summary> /// 获取本地IP地址信息 /// </summary> void G ...
- SQL Server 索引和视图
Ø 索引 1. 什么是索引 索引就是数据表中数据和相应的存储位置的列表,利用索引可以提高在表或视图中的查找数据的速度. 2. 索引分类 数据库中索引主要分为两类:聚集索引和非聚集索引.SQL Serv ...
- windows server 备份与还原
1:文件备份: ①Goodsync ②Acronis Backup & Recovery 2:域控&系统备份 ①CMD -- >NTbackup (不支持异机还原) ②Acron ...
- Oracle小数点格式化
1. select to_char(123456789.12345,'fm999999990.99') from dual; 如果fm后位数不足则会显示 ## select to_char(12345 ...
- jQuery--each遍历使用方法
定义和用法 each() 方法规定为每个匹配元素规定运行的函数. 提示:返回 false 可用于及早停止循环. 语法 $(selector).each(function(index,element)) ...