题目链接:http://poj.org/problem?id=3714

  分治算法修改该为两个点集的情况就可以了,加一个标记。。。

 //STATUS:C++_AC_2094MS_4880KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e30;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Node{
double x,y;
int id,index;
Node(){}
Node(double _x,double _y,int _index):x(_x),y(_y),index(_index){}
}nod[N],temp[N]; int n; double dist(Node &a,Node &b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} bool cmpxy(Node a,Node b)
{
return a.x!=b.x?a.x<b.x:a.y<b.y;
} bool cmpy(Node a,Node b)
{
return a.y<b.y;
} pii Closest_Pair(int l,int r)
{
if(l==r || l+==r)return pii(l,r);
double d,d1,d2;
int i,j,k,mid=(l+r)/;
pii pn1=Closest_Pair(l,mid);
pii pn2=Closest_Pair(mid+,r);
if(pn1.first==pn1.second || nod[pn1.first].index==nod[pn1.second].index)
d1=OO;
else d1=dist(nod[pn1.first],nod[pn1.second]);
if(pn2.first==pn2.second || nod[pn2.first].index==nod[pn2.second].index)
d2=OO;
else d2=dist(nod[pn2.first],nod[pn2.second]); pii ret;
d=Min(d1,d2);
ret=d1<d2?pn1:pn2; for(i=l,k=;i<=r;i++){
if(fabs(nod[mid].x-nod[i].x)<=d){
temp[k++]=nod[i];
}
}
sort(temp,temp+k,cmpy);
for(i=;i<k;i++){
for(j=i+;j<k && fabs(temp[j].y-temp[i].y)<d;j++){
if(dist(temp[i],temp[j])<d && (temp[i].index^temp[j].index)){
d=dist(temp[i],temp[j]);
ret=make_pair(temp[i].id,temp[j].id);
}
}
}
return ret;
} void Init()
{
int i,t;
double x,y;
scanf("%d",&t);
n=t<<;
for(i=;i<t;i++){
scanf("%lf%lf",&x,&y);
nod[i]=Node(x,y,);
}
for(;i<n;i++){
scanf("%lf%lf",&x,&y);
nod[i]=Node(x,y,);
}
sort(nod,nod+n,cmpxy);
for(i=;i<n;i++)nod[i].id=i;
} int main(){
// freopen("in.txt","r",stdin);
int T,i,j;
scanf("%d",&T);
while(T--)
{
Init();
pii ans=Closest_Pair(,n-); printf("%.3lf\n",dist(nod[ans.first],nod[ans.second]));
}
return ;
}

POJ-3714 Raid 平面最近点对的更多相关文章

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

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

  2. POJ 3714 Raid

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

  3. 『Raid 平面最近点对』

    平面最近点对 平面最近点对算是一个经典的问题了,虽然谈不上是什么专门的算法,但是拿出问题模型好好分析一个是有必要的. 给定\(n\)个二元组\((x,y)\),代表同一平面内的\(n\)个点的坐标,求 ...

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

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

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

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

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

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

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

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

  8. POJ 3714 Raid 近期对点题解

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

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

随机推荐

  1. uva 11151

    求最长回文串  就是将字符串翻转后求最长公子列..... #include <cstdio> #include <cstdlib> #include <algorithm ...

  2. uva 10730

    题意:如果数列中没有三个元素的子序列构成等差数列输出yes 不然no 标记每个数出现的位置  然后从0开始寻找三个元素的等差数列  如果这三个元素的位置满足条件则原数列中存在等差数列 #include ...

  3. 一个只需要点 「下一步」就完成监控 Windows

    Cloud Insight 此前已然支持 Linux 操作系统,支持20多中数据库中间件等组件,多种操作,多种搭配,服务器监控玩的其乐无穷啊!但想想还有许多 Windows 的小伙伴没有体验过,所以在 ...

  4. linux配置防火墙详细步骤(iptables命令使用方法)

    通过本教程操作,请确认您能使用linux本机.如果您使用的是ssh远程,而又不能直接操作本机,那么建议您慎重,慎重,再慎重! 通过iptables我们可以为我们的Linux服务器配置有动态的防火墙,能 ...

  5. 万网空间如何安装wordpress

    万网空间如何安装wordpress建站教程 _ 学做网站论坛 http://www.xuewangzhan.com/wpbbs/1643.html   1.先在本地下载一个最新版本的wordpress ...

  6. P127、面试题20:顺时针打印矩阵

    题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字.例如:如果输入如下矩阵:1  2  3  4 5  6  7  89  10  11  1213  14  15  16则依次打印出 ...

  7. 【HDOJ】4305 Lightning

    1. 题目描述当一个结点lightning后,可以向其周围距离小于等于R的结点传播lightning.然后以该结点为中心继续传播.以此类推,问最终形成的树形结构有多少个. 2. 基本思路生成树级数模板 ...

  8. hdu 4502 吉哥系列故事——临时工计划(dp)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4502 思路:一个简单的dp ,比赛时没做出来呢..... d[i]代表 到第i天时的最大值 #includ ...

  9. 宏HASH_DELETE

    HASH_DELETE(buf_page_t, hash, buf_pool->page_hash, fold, bpage); NAME 可理解为 void* next /********** ...

  10. 自定义一个compass可编译的目录结构

    在学习compass的过程中, 根绝文档说明,如果使用compass create myObject命令会创建一个标准的Compass项目目录结构,如下图: 此时如果使用compass compile ...