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. Eclipse调试Bug的七种常用技巧(转)

        注意事项及小结: (1)Line Breakpoint:如果设置Conditional,监控的变量需要比当前行高一级block,譬如for(int i=0;i<20;i++)中的i,fo ...

  2. 基于jeasyui的遮罩扩展[修复链式bug]

    说明和使用方法看下面代码,直接复制下面代码保存为js文件,引用即可. 遮罩效果从datagrid中提取,针对jquery进行优化. 下载地址(附Demo):http://pan.baidu.com/s ...

  3. windows phone中,将crash report记录下来,写入文件,方便分析

    APP出现crash(崩溃)总是不能忍的 当我们连接调试器调试的时候,发现每当APP崩溃的时候 程序都会走到App.xaml.cs中的 // Code to execute on Unhandled ...

  4. bzoj1654 [Usaco2006 Jan]The Cow Prom 奶牛舞会

    Description The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in ...

  5. 在Linux CentOS 6.5 (Final)上安装git-1.9.0

    CentOS 6.5 (Final)默认安装的git版本为1.7.1.3,而我们希望安装1.9.0版本.由于rpm安装库里没有1.9.0版本,因此我们需要找其它方法来安装. 网上有很多文章介绍了如何从 ...

  6. POJ1094 Sorting It All Out(拓扑排序)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30110   Accepted: 10 ...

  7. Spring的MethodInvokingFactoryBean

    通过MethodInvokingFactoryBean 可以向某静态方法注入参数. 如: <bean class="org.springframework.beans.factory. ...

  8. Android学习总结——适配器

    适配器是AdapterView视图(如ListView - 列表视图控件.Gallery - 缩略图浏览器控件.GridView - 网格控件.Spinner - 下拉列表控件.AutoComplet ...

  9. 让你的javascript函数拥有记忆功能,降低全局变量的使用

    考虑例如以下场景:假如我们须要在界面上画一个圆,初始的时候界面是空白的.当鼠标移动的时候,圆须要尾随鼠标移动.鼠标的当前位置就是圆心.我们的实现方案是:假设界面上还没有画圆,那么就新创建一个:假设已经 ...

  10. ubuntu中文实训手册

    http://people.ubuntu.com/~happyaron/udc-cn/lucid-html/ http://www.apachefriends.org/zh_cn/xampp-linu ...