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 ...
随机推荐
- ural 1123
找大于等于原数的最小回文数字 代码比较烂........... #include <iostream> #include <cstdio> #include <cstr ...
- sqlmap动态sql优化,避免传参失误批量修改和删除操作!
分析以下的sqlmap存在问题: <delete id="deletePartspic" parameterClass="TblSpPartspic"&g ...
- QueryPerformanceFrequency 和 QueryPerformanceCounter用法
QueryPerformanceFrequency() - 基本介绍 类型:Win32API 原型:BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFr ...
- 【leetcode】Longest Palindromic Substring (middle) 经典
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- ajax返回正个页面
- Android GridView、ListView、ScrollView上下拉刷新
实现方法是将显示的内容最外层的ViewGroup做成一个LinearLayout,并扩展它,使其可以上下拖动. 重点是实现View的onTouch方法. 下载:http://files.cnblogs ...
- javaweb学习总结(四十五)——监听器(Listener)学习二
一.监听域对象中属性的变更的监听器 域对象中属性的变更的事件监听器就是用来监听 ServletContext, HttpSession, HttpServletRequest 这三个对象中的属性变更信 ...
- C++ eof()函数相关应用技巧分享
C++编程语言中的很多功能在我们的实际应用中起着非常大的作用.比如在对文件文本的操作上,就可以用多种方式来实现.在这里我们介绍的C++ eof()函数就是其中一个比较常用的基本函数. 在使用C/C++ ...
- 智传播客hadoop视频学习笔记(共2天)
第一天:1.答疑解惑• 就业前景• 学习hadoop要有什么基础• hadoop会像塞班一样,热一阵子吗• hadoop学习起来容易还是困难• 课堂上的学习方法(所有实验必须按照要求做,重原 ...
- 高难度(1)常用的AR构架或库
Layar http://www.layar.com/ Layar旨在打造的一个开放的增强现实的平台,任何第三方都可以通过Layar的开发接口来打造基于Layar的自己的增强现实应用. 高通AR开发包 ...