POJ3714+最近点对
来源:互联网 发布:教父 知乎 编辑:程序博客网 时间:2024/04/30 13:38
只需要特判标记即可
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<algorithm>using namespace std;const double eps = 1e-8;const double inf = 9999999999.0;const int maxn = 100005;struct Point{double x,y;int flag;};Point pnt[ maxn<<1 ],temp[ maxn<<1 ];double dis( Point a,Point b ){return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );}int cmpxy( Point a,Point b ){if( a.x!=b.x )return a.x<b.x;elsereturn a.y<b.y;}int cmpx( Point a,Point b ){return a.x<b.x;}int cmpy( Point a,Point b ){return a.y<b.y;}double solve( int L,int R ){if( L==R )return inf;if( L+1==R ){if( pnt[L].flag==pnt[R].flag )return inf;elsereturn dis( pnt[L],pnt[R] );}int mid = (L+R)/2;double res,Ldis,Rdis;Ldis = solve( L,mid );Rdis = solve( mid+1,R );res = min( Ldis,Rdis );int cnt = 0;for( int i=L;i<=R;i++ ){if( fabs(pnt[i].x-pnt[mid].x)<=res ){temp[cnt++] = pnt[i];}}sort( temp,temp+cnt,cmpy );for( int i=0;i<cnt;i++ ){for( int j=i+1;j<cnt;j++ ){if( fabs( pnt[i].y-pnt[j].y )>res ) break;if( pnt[i].flag==pnt[j].flag ) continue;res = min( res,dis(pnt[i],pnt[j]) );}}return res;}int main(){int ca;scanf("%d",&ca);while( ca-- ){int n;scanf("%d",&n);for( int i=0;i<n;i++ ){scanf("%lf%lf",&pnt[i].x,&pnt[i].y);pnt[i].flag = 1;}for( int i=n;i<2*n;i++ ){scanf("%lf%lf",&pnt[i].x,&pnt[i].y);pnt[i].flag = 2;}sort( pnt,pnt+2*n,cmpxy );double Ans = solve( 0,2*n-1 );printf("%.3lf\n",Ans);}return 0;}
- POJ3714+最近点对
- poj3714 最近点对
- POJ3714-最近点对
- POJ3714 最近点对
- poj3714 两个阵营的 平面最近点对
- HDU1007-Quoit Design&POJ3714-Raid(最近点对问题)
- poj3714 Raid 找最近点对问题(二)
- #POJ3714#Raid(不同类最小点对 + 分治)
- 最近点对算法
- 最近点对
- 最近点对问题
- 求最近点对
- 最近点对
- 最近点对问题
- 平面最近点对
- 最近点对
- 求最近点对
- 平面最近点对
- hdu 4310 Hero
- 讨论WebService安全机制的几篇博文
- vim 复制粘贴
- 小糖的糖果
- Lecture 3 (二)
- POJ3714+最近点对
- int Serializable导致的Provided id of the wrong type. Expected异常
- Linux下使用JNI的常见问题及解决方案
- HDU1007+最近点对
- UVA 550 Multiplying by Rotation 数论的规律
- Zend Framework2学习一:介绍
- 黑马程序员----【张孝祥7K面试题】一交通灯管理系统视频学习笔记
- hdu 1010 Tempter of the Bone(dfs+奇偶剪枝)
- 用Visual Studio2010 编译 C++文件"hello world”