版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/。未经本作者同意不得转载。

https://blog.csdn.net/kenden23/article/details/36407517

本题是一般近期对点求解。略微添加点限定:有两个集合点,要求不同集合中的点的近期对。

那么就添加一个推断。假设是同一个集合中的点,那么就返回最大值。其它和一般的近期对点解法一样。

注意:本题数据有重合点。那么就要防止分类的时候溢出。

Geeks上的近期对的程序是无法处理有重合点的情况的。


#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <math.h>
#include <algorithm>
#include <string.h>
using std::sort;
struct Point
{
int x, y;
int whichSet;
}; inline int xCmp(const void *a, const void *b)
{
const Point *a1 = static_cast<const Point *>(a);
const Point *b1 = static_cast<const Point *>(b);
return a1->x - b1->x;
} inline int xCmp2(const Point &a, const Point &b)
{
return a.x < b.x;
} inline int yCmp2(const Point &a, const Point &b)
{
return a.y < b.y;
} inline int yCmp(const void *a, const void *b)
{
const Point *a1 = static_cast<const Point *> (a);
const Point *b1 = static_cast<const Point *> (b);
return a1->y - b1->y;
} inline float dist(Point &a, Point &b)
{
if (a.whichSet == b.whichSet) return FLT_MAX;
float xd = (float)(a.x - b.x);
float yd = (float)(a.y - b.y);
return sqrtf(xd*xd + yd*yd);
} float bruteForce(Point P[], int n)
{
float m = FLT_MAX;
for (int i = 0; i < n; ++i)
{
for (int j = i+1; j < n; ++j)
{
float d = dist(P[i], P[j]);
if (d < m) m = d;
}
}
return m;
} inline float mMin(float x, float y) { return x < y? x : y; } float stripClosest(Point strip[], int n, float d)
{
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n && strip[j].y -strip[i].y < d; j++)
{
float t = dist(strip[i], strip[j]);
if (t < d) d = t;
}
}
return d;
} float closestUtil(Point Px[], Point Py[], int n)
{
if (n <= 3) return bruteForce(Px, n); int m = n >> 1;
Point midPoint = Px[m]; Point *PyL = new Point[m+1];
Point *PyR = new Point[n-m-1];
int le = 0, ri = 0;
for (int i = 0; i < n; i++)
{//修正bug:添加le<m+1推断。防止反复点。引起溢出
if (Py[i].x <= midPoint.x && le < m+1) PyL[le++] = Py[i];
else PyR[ri++] = Py[i];
} float dl = closestUtil(Px, PyL, le);//m+1);
float dr = closestUtil(Px+m+1, PyR, ri);//n-m-1); float d = mMin(dl, dr); Point *strip = new Point[n];
int j = 0;
for (int i = 0; i < n; i++)
{
if (fabsf(float(Py[i].x - midPoint.x)) < d) strip[j++] = Py[i];
}
d = mMin(d, stripClosest(strip, j, d));
delete [] strip;
delete [] PyL;
delete [] PyR;
return d;
} float closest(Point P[], int n)
{
Point *Px = new Point[n];
Point *Py = new Point[n];
memcpy(Px, P, n * sizeof(Point));
memcpy(Py, P, n * sizeof(Point)); //qsort(Px, n, sizeof(Point), xCmp);
sort(Px, Px+n, xCmp2);
//qsort(Py, n, sizeof(Point), yCmp);
sort(Py, Py+n, yCmp2); float d = closestUtil(Px, Py, n);
delete [] Px;
delete [] Py;
return d;
} int main()
{
int T, n;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
Point *P = new Point[n<<1];
for (int i = 0; i < n; i++)
{
scanf("%d %d", &P[i].x, &P[i].y);
P[i].whichSet = 1;
}
for (int i = n; i < (n<<1); i++)
{
scanf("%d %d", &P[i].x, &P[i].y);
P[i].whichSet = 2;
}
printf("%.3f\n", closest(P, n<<1));
delete [] P;
}
return 0;
}

POJ 3714 Raid 近期对点题解的更多相关文章

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

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

  2. POJ 3714 Raid(平面近期点对)

    解题思路: 分治法求平面近期点对.点分成两部分,加个标记就好了. #include <iostream> #include <cstring> #include <cst ...

  3. poj 3714 Raid【(暴力+剪枝) || (分治法+剪枝)】

    题目:  http://poj.org/problem?id=3714 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27048#prob ...

  4. POJ 3714 Raid

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

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

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

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

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

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

  8. 【POJ 3714】Raid

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

  9. 【POJ 3714】 Raid

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

随机推荐

  1. workflow engine Ruote初体验之一(概念)

    由于最近自己写点小东西,需要有工作流程管理方面的应用,所有的环境为Ruby on rails,所有在选择流程引擎的时候选择了ruote,但是对于ruote是完全陌生的,所以在这里记下点滴,如果理解的不 ...

  2. supervisor开机自启动方法

    配置service类型服务 #!/bin/bash # # supervisord This scripts turns supervisord on # # Author: Mike McGrath ...

  3. GIS可视化

    作为一名GIS专业的学生,一晃也毕业三年了,在supermap也呆了三年多了,做的最多的就是浏览器端的GIS展示,最近也想分享一下我们团队在浏览器端GIS可视化的一些成果,算是做个宣传吧!有用的着的可 ...

  4. HTML5 Canvas 绘制新西兰国旗

    代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...

  5. ZT:成熟是一种明亮而不刺眼的光辉

    成熟是一种明亮而不刺眼的光辉, 一种圆润而不腻耳的音响, 一种不再需要对别人察言观色的, 一种终于停止向周围申诉求告的大气, 一种不理会哄闹的微笑, 一种洗刷了偏激的冷漠, 一种无需声张的厚实, 一种 ...

  6. 数据结构之---C语言实现图的数组(邻接矩阵)存储表示

    //图的数组(邻接矩阵)存储表示 #include <stdio.h> #include <stdlib.h> #define MAX_VEX_NUM 50 typedef c ...

  7. xshell容易断开的问题

    修改服务器的sshd_config文件. http://bbs.51cto.com/thread-904289-1.html

  8. UDP通信注意事项

    今天调试UDP,笔记本上面可以实现但台式机上面竟然无法通信,后来找了半天,原来是权限问题.必须将用户权限设置为最低才行. 在运行里面输入UAC (user access control )用户权限设置 ...

  9. Spring2.5学习3.2_编码剖析@Resource注解的实现原理

    首先看一下J2EE提供的@Resource注解:该注解默认安照名称进行装配,名称能够通过name属性进行指定, 假设没有指定name属性,当注解写在字段上时,默认取字段名进行依照名称查找,假设注解写在 ...

  10. Web Service之Soap请求响应内容中文编码解密

    java模拟Soap请求测试Web Service接口,发现Web Service响应内容中的中文竟然是编码格式.比如: 中文:退保成功 Soap中文编码:退保成功   我仔细分析后发现,退编码实际上 ...