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. 其中 (%{WORD:x_forword}|-) |表示或的意思

    121.40.205.143 [30/Aug/2016:14:03:08 +0800] "GET /resources/images/favicon.ico HTTP/1.1" - ...

  2. 【转】高通平台android 环境配置编译及开发经验总结

    原文网址:http://blog.csdn.net/dongwuming/article/details/12784535 1.高通平台android开发总结 1.1 搭建高通平台环境开发环境 在高通 ...

  3. bzoj1623 [Usaco2008 Open]Cow Cars 奶牛飞车

    Description   编号为1到N的N只奶牛正各自驾着车打算在牛德比亚的高速公路上飞驰.高速公路有M(1≤M≤N)条车道.奶牛i有一个自己的车速上限Si(l≤Si≤1,000,000).     ...

  4. bzoj1644 [Usaco2007 Oct]Obstacle Course 障碍训练课

    Description 考虑一个 N x N (1 <= N <= 100)的有1个个方格组成的正方形牧场.有些方格是奶牛们不能踏上的,它们被标记为了'x'.例如下图: . . B x . ...

  5. 理解*ptr++

    这是C语言中指针的基本用法之一,我们先来看一个小例子.下面是代码: int main(void) { char *p = "Hello"; while(*p++) printf(& ...

  6. Skulpt

    Skulpt Python. Client side. Skulpt is an entirely in-browser implementation of Python. No preprocess ...

  7. REDHAT、CenterOS使用安装Linux系统时的光盘镜像来安装软件

    使用安装Linux系统时的光盘镜像来安装软件 (1)以虚拟机上,安装mysql为例: 查看mysql是否安装 rpm -qa|grep -i mysql    显示下面,证明mysql已安装客户端,下 ...

  8. CodeForces 10D. LCIS 最长公共上升子序列模板题 + 打印路径

    推荐一篇炒鸡赞的blog. 以下代码中有打印路径. #include <algorithm> #include <iostream> #include <cstring& ...

  9. ARM9嵌入式学习笔记(2)-Vi使用

    ARM9嵌入式学习笔记(2) 实验1-1-3 Vi使用 vi创建文件vi hello.c:vi smb.conf-打开文件smb.conf i键-插入模式:esc键-命令行模式::-底行模式: 底行模 ...

  10. 批处理Bat实现整合定时关机或取消定时关机

    @echo off :start choice /c:12 /m "输入1为设置定时关机,2为取消定时关机: " if errorlevel 2 goto cancel if er ...