解题思路:

分治法求平面近期点对。点分成两部分,加个标记就好了。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <iomanip>
#include <string.h>
#define LL long long
using namespace std;
const int MAXN = 200000 + 10;
const double INF = 1e100;
struct Point
{
double x, y;
int flag;
}P[MAXN];
int N;
Point vec[MAXN];
bool cmp_x(Point a, Point b)
{
return a.x < b.x;
}
bool cmp_y(Point a, Point b)
{
return a.y < b.y;
}
double dis(Point a, Point b)
{
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);
}
double solve(Point *a, int l, int r)
{
if(l == r) return INF;
if(l + 1 == r)
{
if(P[l].flag == P[r].flag)
return INF;
return dis(P[l], P[r]);
}
int m = (l + r) >> 1;
double d = solve(a, l, m);
d = min(d, solve(a, m + 1, r));
int sz = 0;
for(int i=l;i<=r;i++)
{
if(fabs(P[i].x - P[m].x) <= d)
vec[sz++] = P[i];
}
sort(vec, vec + sz, cmp_y);
for(int i=0;i<sz;i++)
{
for(int j=i+1;j<sz;j++)
{
if(fabs(vec[i].y - vec[j].y) >= d)
break;
if(vec[i].flag != vec[j].flag)
{
double rs = dis(vec[i], vec[j]);
if(rs < d) d = rs;
}
}
}
return d;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &N);
for(int i=0;i<N;i++)
{
scanf("%lf%lf", &P[i].x, &P[i].y);
P[i].flag = 0;
}
for(int i=0;i<N;i++)
{
scanf("%lf%lf", &P[i + N].x, &P[i + N].y);
P[i + N]. flag = 1;
}
N <<= 1;
sort(P, P + N, cmp_x);
double ans = solve(P, 0, N - 1);
printf("%.3f\n", ans);
}
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(平面最近点对)

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

  3. POJ 3714 Raid 近期对点题解

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

  4. POJ 3714 Raid

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

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

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

  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 [算法] 分治求平面最近点对 [代码] #include <algorithm> #include <bi ...

  9. 【POJ 3714】Raid

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

随机推荐

  1. C# C++ 字符串传递

    C# C++ 字符串传递 标签: c#c++bytestring测试c 2012-06-14 17:425707人阅读评论(3)收藏举报 分类: C#(11)  作者同类文章X C++(112)  作 ...

  2. 下次自己主动登录(记住password)功能

    1:进入cookie插件 <script src="jquery.cookie.js" type="text/javascript"></sc ...

  3. BingMap频繁Add Pushpin和Delete Pushpin会导致内存泄露

    近期在做性能測试的时候发现BingMap内存泄露(memory leak)的问题,查找了一些国外的帖子,发现也有类似的问题,可是没有好的解决的方法. https://social.msdn.micro ...

  4. 关于HEXO安装失败的解决方法

    目前国内npm源有问题:所以键入如下代码即可安装成功: npm install -g cnpm --registry=https://registry.npm.taobao.org cnpm inst ...

  5. 毕业两年做到测试经理的经历总结- 各个端的自动化,性能测试结合项目具体场景实战,分析客户反馈的Bug

    前言 最近看到行业的前辈都分享一些过往的经历来指导我们这些测试人员,我很尊敬我们的行业前辈,没有他们在前面铺路,如今我们这帮年轻的测试人估计还在碰壁或摸着石头过河,结合前辈们的经验,作为年轻的测试人也 ...

  6. PHP的工作原理和生命周期

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u013778883/article/details/79831035   php是一门适用于web开 ...

  7. P2P系统哪家强,功能其实都一样

    现在的P2P平台有好几千家了,了解了其中的几十家,发现用户端的P2P界面功能都差不多.下面来做个简要的总结: 1.通用功能  注册.登录  2.投资理财  针对理财人的投标.债权转让  3.借款申请  ...

  8. imresize() 函数——matlab

    功能:改变图像的大小. 用法:B = imresize(A,m)B = imresize(A,m,method)B = imresize(A,[mrows ncols],method) B = imr ...

  9. word中公式的排版及标题列表

    1.首先建好你的标题,如标题1,标题2等等,你能够依次改变它们的字体,段落等格式,新建格式例如以下图所看到的 红圈处即建立新的格式,你能够建立不论什么你想要的格式,非常方便: 2.当你建立好了多个标题 ...

  10. [PostgreSQL] Use Foreign Keys to Ensure Data Integrity in Postgres

    Every movie needs a director and every rented movie needs to exist in the store. How do we make sure ...