题意:有n个点,问其中某一对点的距离最小是多少

分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最小值

POJ 3714

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm> const int N = 1e5 + 5;
const double INF = 1e100;
struct Point {
double x, y;
bool flag;
bool operator < (const Point &rhs) const {
return x < rhs.x;
}
};
Point point[N*2];
int idy[N*2];
int n; bool cmp_y(int i, int j) {
return point[i].y < point[j].y;
} double squ(double x) {
return x * x;
} double get_dist(Point &a, Point &b) {
if (a.flag == b.flag) {
return INF;
}
return sqrt (squ (a.x - b.x) + squ (a.y - b.y));
} double min_dist(int left, int right) {
if (left == right) {
return INF;
}
else if (right - left == 1) {
return get_dist (point[left], point[right]);
} else {
int mid = left + right >> 1;
double ret = std::min (min_dist (left, mid), min_dist (mid + 1, right));
if (ret == 0) {
return ret;
}
int endy = 0;
for (int i=mid; i>=left&&point[mid].x-point[i].x<=ret; --i) {
idy[endy++] = i;
}
for (int i=mid+1; i<=right&&point[i].x-point[mid+1].x<=ret; ++i) {
idy[endy++] = i;
}
std::sort (idy, idy+endy, cmp_y);
for (int i=0; i<endy; ++i) {
for (int j=i+1; j<endy&&point[j].y-point[i].y<ret; ++j) {
ret = std::min (ret, get_dist (point[i], point[j]));
}
}
return ret;
}
} int main() {
int T; scanf ("%d", &T);
while (T--) {
scanf ("%d", &n);
for (int i=0; i<2*n; ++i) {
scanf ("%lf%lf", &point[i].x, &point[i].y);
if (i < n) {
point[i].flag = false;
} else {
point[i].flag = true;
}
}
std::sort (point, point+2*n);
printf ("%.3f\n", min_dist (0, 2 * n - 1));
} return 0;
}

HDOJ 1007

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm> const int N = 1e5 + 5;
const double INF = 1e100;
struct Point {
double x, y;
};
Point point[N], py[N];
int n; bool cmp_x(const Point &a, const Point &b) {
return a.x < b.x;
}
bool cmp_y(const Point &a, const Point &b) {
return a.y < b.y;
} double squ(double x) {
return x * x;
} double get_dist(Point &a, Point &b) {
return sqrt (squ (a.x - b.x) + squ (a.y - b.y));
} double min_dist(int left, int right) {
if (left + 1 == right) {
return get_dist (point[left], point[right]);
} else if (left + 2 == right) {
return std::min (get_dist (point[left], point[left+1]),
std::min (get_dist (point[left], point[right]), get_dist (point[left+1], point[right])));
} else {
int mid = left + right >> 1;
double ret = std::min (min_dist (left, mid), min_dist (mid + 1, right));
int cnt = 0;
for (int i=mid; i>=left&&point[mid].x-point[i].x<=ret; --i) {
py[cnt++] = point[i];
}
for (int i=mid+1; i<=right&&point[i].x-point[mid+1].x<=ret; ++i) {
py[cnt++] = point[i];
}
std::sort (py, py+cnt, cmp_y);
for (int i=0; i<cnt; ++i) {
for (int j=i+1; j<cnt&&py[j].y-py[i].y<ret; ++j) {
ret = std::min (ret, get_dist (py[i], py[j]));
}
}
return ret;
}
} int main() {
while (scanf ("%d", &n) == 1) {
if (!n) {
break;
}
for (int i=0; i<n; ++i) {
scanf ("%lf%lf", &point[i].x, &point[i].y);
}
std::sort (point, point+n, cmp_x);
printf ("%.2f\n", min_dist (0, n - 1) / 2);
} return 0;
}

  

最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design的更多相关文章

  1. Hdoj 1007 Quoit Design 题解

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

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

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

  3. (洛谷 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 ...

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. GridView如何合并同类项

    /// <summary>     /// 合并GridView中某列相同信息的行(单元格)     /// </summary>     /// <param name ...

  2. October 9th 2016 Week 41st Sunday

    No matter how resourceful you are, you can't fight fate. 人纵有万般能耐,终也敌不过天命. I find that I gradually be ...

  3. JavaScript对象与数组

    一.Object 类型到目前为止,我们使用的引用类型最多的可能就是 Object 类型了.虽然 Object 的实例不具备多少功能,但对于在应用程序中的存储和传输数据而言,它确实是非常理想的选择.创建 ...

  4. iOS真机调试

    备注:本阶段之前的修改配置文件.准备脚本等,只需要做一次.但本阶段的操作,对每个需要真机调试的工程都要做一遍. ① 禁用Xcode自动的签名操作 将工程配置“Build Settings”中所有的Co ...

  5. Java Web进阶——Filter过滤器

    一.过滤器的介绍: 在Servlet规范2.3中定义了过滤器,它是一个服务器端的组件,可以截取用户端的请求与响应信息,并且对这些信息进行过滤. Servlet过滤器本身并不生成请求和响应对象,只是提供 ...

  6. NYOJ题目96 n-1位数

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAscAAAJgCAIAAADpjVkvAAAgAElEQVR4nO3du04jS/gv7H0T5FwIsa ...

  7. JavaScript基础——实现循环

    循环是多次执行同一段代码的一种手段.当你需要在一个数组或对象集上重复执行相同的任务时,这是非常有用的. JavaScript提供执行for和while循环的功能. 1.while循环 JavaScri ...

  8. MVC学习笔记--跟小静学MVC相关语法特性小补习

    http://www.cnblogs.com/janes/archive/2012/10/15/2721101.html http://www.cnblogs.com/h82258652/p/4795 ...

  9. oracle一点记录

    查看数据库实例名(SERVICE_NAME): sql: select instance_name from v$instance; 如何知道oracle客户端是32位还是64的.windows下启动 ...

  10. 用with实现python的threading,新鲜啊

    哈哈,2.5以后可用.自动加锁释放,如同操作文件打开关闭一样. #!/usr/bin/env python # -*- coding: utf-8 -*- import threading impor ...