POJ 3714 Raid 近期对点题解
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 近期对点题解的更多相关文章
- 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design
题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...
- POJ 3714 Raid(平面近期点对)
解题思路: 分治法求平面近期点对.点分成两部分,加个标记就好了. #include <iostream> #include <cstring> #include <cst ...
- poj 3714 Raid【(暴力+剪枝) || (分治法+剪枝)】
题目: http://poj.org/problem?id=3714 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27048#prob ...
- POJ 3714 Raid
Description After successive failures in the battles against the Union, the Empire retreated to its ...
- poj 3714 Raid(平面最近点对)
Raid Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7473 Accepted: 2221 Description ...
- POJ 3714 Raid(计算几何の最近点对)
Description After successive failures in the battles against the Union, the Empire retreated to its ...
- (洛谷 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 ...
- 【POJ 3714】Raid
[题目链接]:http://poj.org/problem?id=3714 [题意] 给你两类的点; 各n个; 然后让你求出2*n个点中的最近点对的距离; 这里的距离定义为不同类型的点之间的距离; [ ...
- 【POJ 3714】 Raid
[题目链接] http://poj.org/problem?id=3714 [算法] 分治求平面最近点对 [代码] #include <algorithm> #include <bi ...
随机推荐
- html特殊字符编码问题导致的细节问题
今天在写前端html时,一个a标签的链接地址,由于链接地址需要给后台传参数,因此带了部分url参数: 在html源码里写的连接地址是: http://域名/bidder/noticesearch?no ...
- 基于Bootstrap的页面排版知识
标题: Bootstrap定义了所有HTML的标题样式,<h1>...<h6>标签或者在标签内加入.h1 class等可以得到一样的效果 效果: 副标题: 标签<smal ...
- web.xml文件的 xsd引用(或dtd引用)学习
1. 为什么web.xml会有不同版本的xsd引用: JDK依赖变化: 或 servlet(JAVA EE)自身API的改变: 2. 为什么会有dtd和xsd两个版本的区别 我是在这篇文章中看到的,作 ...
- Taskaffinity属性使用小结
TaskAffinity属性小结 最近在项目中用到了TaskAffinity属性,发现这个还是挺有意思,可以用来控制activity所属的任务栈.但同时只设置这一个属性又是不能完成功能的,需要与其它属 ...
- android 内存泄漏出现的情况
非静态内部类的静态实例由于内部类默认持有外部类的引用,而静态实例属于类.所以,当外部类被销毁时,内部类仍然持有外部类的引用,致使外部类无法被GC回收.因此造成内存泄露. 类的静态变量持有大数据对象静态 ...
- PCA原理
http://blog.csdn.net/shizhixin/article/details/51181379
- EasyMvc入门教程-基本控件说明(12)栏目导航
栏目导航一般用来显示当前页面所在的模块层级位置关系,如下图所示: 当然也有前端网站作为小栏目导航,凡是没有绝对,只要不违和就好:),下面上代码: @{ var data = new List<N ...
- 数据挖掘-MovieLens数据集_电影推荐_亲和性分析_Aprioro算法
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Feb 7 14:38:33 201 ...
- linux中expr用法
名称:expr ### 字串长度 shell>> expr length "this is a test" 14 ### 数字商数 shell>> ...
- 报错: Access restriction: The type JPEGImageEncoder is not accessible due to restriction on required library
报错: Access restriction:The type JPEGCodec is not accessible due to restriction on required library C ...