参考自《编程之美》169页,大概原理就是把区间分成两部分,然后递归找每一部分中最近的点对,还有一种情况就是这个点对分属于这两部分,然后选两部分中的部分点枚举即可,取其最小值。

//2013-10-21-17.18
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <iostream>
const double INF=1e100; using namespace std; struct node
{
double x, y;
bool flag;
}p[200005], tmp[200005]; bool cmpx(node a, node b)
{
return a.x < b.x;
} bool cmpy(node a, node b)
{
return a.y < b.y;
} double dis(node a, node b)
{
return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
} double getans(int l, int r)
{
double rs = INF;
if (l == r)
return rs;
if (r - 1 == l)
{
if (p[l].flag == p[r].flag)
return rs;
return dis(p[l], p[r]);
}
int mid = (l+r)>>1;
rs = getans(l, mid);
rs = min(rs, getans(mid+1, r));
int cnt = 1;
for (int i = l; i <= r; i++)
{
if (fabs(p[i].x - p[mid].x) <= rs)
tmp[cnt++] = p[i];
}
sort(tmp+1, tmp+cnt, cmpy); for (int i = 0; i < cnt; i++)
{
for (int j = i+1; j < cnt; j++)
{
if (fabs(tmp[i].y-tmp[j].y) >= rs)
break;
if (tmp[i].flag != tmp[j].flag)
rs = min(rs, dis(tmp[i], tmp[j]));
}
}
return rs;
} int main()
{
int t, n;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
int i = 1;
for (; i <= n; i++)
{
scanf("%lf %lf", &p[i].x, &p[i].y);
p[i].flag = false;
}
n <<= 1;
for (; i <= n; i++)
{
scanf("%lf %lf", &p[i].x, &p[i].y);
p[i].flag = true;
}
sort(p+1, p+n+1, cmpx);
printf("%.3lf\n", getans(1, n));
}
return 0;
}

poj 3714 寻找最近点对的更多相关文章

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

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

  2. (洛谷 P1429 平面最近点对(加强版) || 洛谷 P1257 || Quoit Design HDU - 1007 ) && Raid POJ - 3714

    这个讲的好: https://phoenixzhao.github.io/%E6%B1%82%E6%9C%80%E8%BF%91%E5%AF%B9%E7%9A%84%E4%B8%89%E7%A7%8D ...

  3. 编程之美 set 6 寻找最近点对

    这道题在算法课上当做例题讲过, 当时的印象也比较深 另有一道近似算法的题也在算法课上讲过, 并且印象更深, 复习的时候完全没管, 以为志在必得, 结果真考了那道近似算法, 我却没能打出来 为避免阴沟翻 ...

  4. poj 3714 Raid(平面最近点对)

    Raid Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7473   Accepted: 2221 Description ...

  5. POJ 3714 Raid(计算几何の最近点对)

    Description After successive failures in the battles against the Union, the Empire retreated to its ...

  6. POJ 3714 分治/求平面最近点对

    第一次见这种问题直接懵圈...没想到分治法这么强大,借鉴了lyd的代码: 代码如下 #include<cstdio> #include<algorithm> #include& ...

  7. 【POJ 3714】 Raid

    [题目链接] http://poj.org/problem?id=3714 [算法] 分治求平面最近点对 [代码] #include <algorithm> #include <bi ...

  8. 【POJ 3714】Raid

    [题目链接]:http://poj.org/problem?id=3714 [题意] 给你两类的点; 各n个; 然后让你求出2*n个点中的最近点对的距离; 这里的距离定义为不同类型的点之间的距离; [ ...

  9. POJ 3714 Raid

    Description After successive failures in the battles against the Union, the Empire retreated to its ...

随机推荐

  1. Apicloud 接入海康摄像头

    1准备工作 , 加载apicloud 海康视频模块. 引入 SDK 重新生成项目测试 再config.xml写入appid 话不多说直接上代码 video=api.require("haik ...

  2. c#基础三

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  3. python初识(2)

    1 字符串格式占位符 1.1%s %d %% 占位符 预留 字符串 整型 (转义) name = input('name:') print ('你的名字是:%s'%(name)) 1.2 f" ...

  4. lleetcode 1 two sum c++

    Problem describe:https://leetcode.com/problems/two-sum/ Given an array of integers, return indices o ...

  5. kuangbin专题 专题一 简单搜索 Fire Game FZU - 2150

    题目链接:https://vjudge.net/problem/FZU-2150 题意:’ . '代表火无法烧着的地方,‘ # ’表示草,火可以烧着.选择任意两个‘ # ’(可以两个都选同一个 ‘ # ...

  6. Excel中RATE函数的Java实现

    public class RATE { /** * calculateRate:类excel中的RATE函数,计算结果值为月利率,年华利率 需*12期. <br/> * rate = ca ...

  7. python接口自动化(三十四)-封装与调用--函数和参数化(详解)

    简介 前面虽然实现了参数的关联,但是那种只是记流水账的完成功能,不便于维护,也没什么可读性,随着水平和技能的提升,再返回头去看前边写的代码,简直是惨不忍睹那样的代码是初级入门的代码水平都达不到.接下来 ...

  8. android_sdcard读写(三)

    这次来个稍微复杂点的. package cn.com.sxp;import android.app.Activity;import android.app.ProgressDialog;import ...

  9. 基于IdentityServer4的OIDC实现单点登录(SSO)原理简析

    写着前面 IdentityServer4的学习断断续续,兜兜转转,走了不少弯路,也花了不少时间.可能是因为没有阅读源码,也没有特别系统的学习资料,相关文章很多园子里的大佬都有涉及,有系列文章,比如: ...

  10. STM32F072从零配置工程-串口USART配置

    也是使用HAL库进行配置,通过STMCube生成代码,可以通过这个简单的配置过程看到STMCube生成代码的一种规范: 从main函数入手观察其外设配置结构: 首先是HAL_Init()进行所有外设的 ...