http://acm.hdu.edu.cn/showproblem.php?pid=1007

给出n个玩具(抽象为点)的坐标 求套圈的半径 要求最多只能套到一个玩具

实际就是要求最近的两个坐标的距离

典型的最近点对问题

最近点对详解

http://blog.csdn.net/lonelycatcher/article/details/7973046

//最近点对
# include <stdio.h>
# include <algorithm>
# include <math.h>
# define MAX 100005
# define INF 100000000
using namespace std; struct POINT
{
double x, y;
}arr[MAX]; int n, temp[MAX]; bool cmp(const POINT& a, const POINT& b)
{
if(a.x != b.x)
return a.x < b.x;
return a.y < b.y;
} bool cmp2(const int& a, const int& b)
{
return arr[a].y < arr[b].y;
} double dis(int a, int b)
{
return sqrt(pow(arr[a].x - arr[b].x, 2) + pow(arr[a].y - arr[b].y, 2));
} double min(double a, double b)
{
return a<b?a:b;
} double closest_pointpair(int left, int right)
{
double d = INF; if(left == right)//单个点不计算直接返回INF
return d;
if(left + 1 == right)//相邻点直接计算距离
return dis(left, right); int mid = (left + right) / 2;//取区间中值
d = min(closest_pointpair(left, mid), closest_pointpair(mid+1, right));//分治 取二者中的小数作为领域范围 int num = 0;
for(int i = left; i <= right; i++)//记录中值两边+(-)d范围内的所有点的下标
if(fabs(arr[mid].x - arr[i].x) <= d)
temp[num++] = i; sort(temp, temp+num, cmp2);//按y从小到大排序 for(int i = 0; i < num; i++)//两两枚举 若距离小于d 替换
{
for(int j = i + 1; j < num && arr[temp[j]].y - arr[temp[i]].y < d; j++)
{
double dt = dis(temp[i], temp[j]);
d = min(d, dt);
}
} return d;//返回当前分域最近点对距离
} int main()
{
while(scanf("%d", &n) && n)
{
for(int i = 0; i < n; i++)
scanf("%lf %lf",&arr[i].x, &arr[i].y); sort(arr, arr+n, cmp); printf("%.2lf\n", closest_pointpair(0, n-1) / 2);
} return 0;
}

HDOJ-1007 Quoit Design(最近点对问题)的更多相关文章

  1. 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design

    题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...

  2. 杭电OJ——1007 Quoit Design(最近点对问题)

    Quoit Design Problem Description Have you ever played quoit in a playground? Quoit is a game in whic ...

  3. hdu 1007 Quoit Design (最近点对问题)

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. Hdoj 1007 Quoit Design 题解

    Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...

  5. HDU 1007 Quoit Design最近点对( 分治法)

    题意: 给出平面上的n个点,问任意点对之间的最短距离是多少? 思路: 先将所有点按照x坐标排序,用二分法将n个点一分为二个部分,递归下去直到剩下两或一个点.对于一个部分,左右部分的答案分别都知道,那么 ...

  6. HDU 1007 Quoit Design(经典最近点对问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1007 Quoit Design Time Limit: 10000/5000 MS (Java/Oth ...

  7. HDU 1007 Quoit Design【计算几何/分治/最近点对】

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. hdu 1007 Quoit Design 分治求最近点对

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  9. HDU 1007 Quoit Design(二分+浮点数精度控制)

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  10. poj 1007 Quoit Design(分治)

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

随机推荐

  1. C# DateTime类,TimeSpan类

    DateTime类是.Net中用于处理时间类型数据的. 一.字段 MaxValue 表示 DateTime 的最大可能值.此字段为只读. MinValue     表示 DateTime 的最小可能值 ...

  2. C# Struct结构体

    C#中结构类型和类类型在语法上非常相似,他们都是一种数据结构,都可以包括数据成员和方法成员. 结构和类的区别: 1.结构是值类型,它在栈中分配空间:而类是引用类型,它在堆中分配空间,栈中保存的只是引用 ...

  3. 使用 asp.net Web API 2的坑

    使用工具: Googl  浏览器+PostMan 插件 写了个  控制器 添加了个Action,结果呢?GET 方式请求没问题. POST一直,在服务器端获取不了参数...找了官方的文档 .各种雨里雾 ...

  4. java数组复制的方式和效率比较

    java中,数组的复制有以下三种方式: 1. 调用System.arraycopy(Arrays.copyOfRange可以当作第四种,但是底层调用的是System.arraycopy,所以,认为是同 ...

  5. archlinux相关资料整理

    Arch linux Arch Linux Wiki Arch linux Wiki Markdown Arch Wiki python continuing ...

  6. myeclipse修改tomcat端口

    myeclipse自带的tomcat可以直接在myeclipse下面进行配置,但是如果是配置的tomcat 只能自己手动修改了

  7. EasyUI DataGrid定制默认属性名称

    EasyUI DataGrid绑定服务器返回Json数据的解决方案 1. 服务器返回的数据对象格式,及初始化返回值 public class RequestResult { private int c ...

  8. Stones(优先队列)

    Stones Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Subm ...

  9. 数据库的优化tips

    数据库   TIPS:: 1.用于记录或者是数据分析的表创建时::使用Id作为主键,1,2,3...表示消息条数.用户账号id用于做外键.一个用户相应唯一个accountId             ...

  10. 网络编程API-下 (I/O复用函数)

    IO复用是Linux中的IO模型之中的一个,IO复用就是进程预先告诉内核须要监视的IO条件,使得内核一旦发现进程指定的一个或多个IO条件就绪,就通过进程进程处理.从而不会在单个IO上堵塞了. Linu ...