POJ-3714 Raid 平面最近点对
题目链接: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 平面最近点对的更多相关文章
- 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design
题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...
- POJ 3714 Raid
Description After successive failures in the battles against the Union, the Empire retreated to its ...
- 『Raid 平面最近点对』
平面最近点对 平面最近点对算是一个经典的问题了,虽然谈不上是什么专门的算法,但是拿出问题模型好好分析一个是有必要的. 给定\(n\)个二元组\((x,y)\),代表同一平面内的\(n\)个点的坐标,求 ...
- 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 ...
- 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 近期对点题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- (洛谷 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 ...
随机推荐
- 用IDEA调试Play工程
IDEA的版本是14.0.1,运行在MAC OS X Yosemite上. IDEA已经装了Scala插件,但是在新建工程中,Scala的选项中并没有Play框架,不知道什么原因. 导入Play工程 ...
- hdu 1847 Good Luck in CET-4 Everybody! 博弈论
方法一:找规律,很容易知道 #include<stdio.h> int main(){ int n; while(scanf("%d",&n)!=EOF){ p ...
- HandlerThread 类的学习(转载)
HandlerThread继承于Thread,所以它本质就是个Thread.HandlerThread类用于方便的创建一个含有looper的线程类,looper用来创建handler类.我们一般不创建 ...
- 镜面电火花EDM加工技术资料,模具行业的人应该好好看看!
目前镜面电火花加工技术在精密型腔模具制造中逐步得以推广.本文就企业中镜面电火花加工应用的关键环节,结合实践分析了影响镜面加工性能的因素.通过控制各个工艺环节,可有效实现高质量.高效率的镜面电火花加工. ...
- Android 遍历sdcard中指定文件夹下的图片(jpg,jpeg,png)
File scanner5Directory = new File(Environment.getExternalStorageDirectory().getPath() + "/scann ...
- Servlet 下载文件
这几天有点懒散,还好没有忘记看书,上周去了国家图书馆翻阅了一些和Java相关的书籍,其实这些书都是自己以前看过或者听过,按理来说,不应该看自己已经看过的书籍,应该找一些最新的书籍去看,但是每次走到书架 ...
- 在vs2010中mfc,C++的一些小经验
1 如果你最近才从vc6.0到vs2010,在vs2010中mfc可能遇见一个小问题,如果你添加或改天了窗口中的控件,运行程序缺没有发现其中的变化,这时候需要在debug选项中rebuild all一 ...
- Emulator control为灰色的情况
新建了一个虚拟机,然后发现Emulator control为灰色,让eclipse重启下就可以了,然后就可以使用了.
- Poj2948Martian Mining(记忆化)
链接 这题意好难懂 看得迷迷糊糊 想的也迷迷糊糊 后来睡了会突然想到了..不就是类似以前的矩阵操作 从右下角记忆化 大的由小的推来 dp[i][j] = max(dp[i-1][j]+s1,dp ...
- C#控件背景透明的几种解决方案
已经很少做winform程序了,最新参与了一个小项目,遇到了控件背景透明的功能要求,特在此总结一下,供有需要的同行参考. 0.背景透明的概念和分类 背景透明是啥意思呢,就是背景透明.哈哈,废话了.其实 ...