版权声明:本文作者靖心,靖空间地址: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. windows service 调试

    在程序运行入口调试 #if DEBUG Debugger.Launch(); #endif

  2. 【java】StringBuilder的三种清除方法对比

    参考链接:https://blog.csdn.net/roserose0002/article/details/6972391

  3. /etc/shadow 密码加密方法

    [root@mysql-master ~]# cat /etc/shadowroot:$6$spzQDWctb8Lmju0o$KoUz5Qwv1tWyVYfd5cuBw.TQVIaCvCX8ixGG9 ...

  4. 聚合数据 iOS 项目开发实战:条码查询器

    记录下,聚合数据 iOS 项目开发实战:条码查询器:视频地址:http://www.jikexueyuan.com/course/324.html 条码查询API:https://www.juhe.c ...

  5. PhoneNumber

    项目地址:PhoneNumber 简介:一个获取号码归属地和其他信息(诈骗.骚扰等)的开源库   一个获取号码归属地和其他信息(诈骗.骚扰等)的开源库.支持本地离线(含归属地.骚扰.常用号码)和网络( ...

  6. 出现“Windows资源管理器已停止工作”错误

    出现"Windows资源管理器已停止工作"错误 什么是资源管理器呢,explorer.exe进程的作用就是让我们管理计算机中的资源! 今天开电脑的时候就一直提示windows资源管 ...

  7. boost库shared_ptr实现桥接模式

    主程序 /*将抽象部分与实现部分分离,使它们都能够独立的变化*/ #include "bridge.h" int main() { cout <<"main ...

  8. SAS学习经验总结分享:篇五-过程步的应用

    之前已经介绍过BASE SAS分为数据步和过程步,过程步是对数据步生成的数据集进行分析和处理,并挖掘数据信息,写出分析报告做总结评价. (本文为原创,禁止复制或转载,转载务必标明出处:http://w ...

  9. Linux安装indicator-china-weather

    https://launchpad.net/indicator-china-weather sudo apt-get update sudo apt-get install python-appind ...

  10. iscroll的理解

    1. 最佳的HTML结构如下: <div id="wrapper"> <ul> <li>...</li> <li>... ...