UVA 10245 The Closest Pair Problem
来源:互联网 发布:mac steam 游戏存档 编辑:程序博客网 时间:2024/05/23 10:27
n=1的情况要注意~
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<cmath>using namespace std;const int maxn = 10060;// 分治算法求最近点对struct point//保存每一个点{ double x,y;}p[maxn];int a[maxn];//保存筛选的坐标点的索引即2min(dl,dh)区间的坐标点索引int cmpx(point a,point b)//对n个点按横坐标由小到大排序{ return a.x<b.x;}int cmpy(int a,int b)//(这里用的是下标索引)对筛选的点按纵坐标由小到大排序{ return p[a].y<p[b].y;}inline double dis(point a,point b)//求点对间的距离{ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}double closest(int low,int high)//求最近点对{ int i,j,k; if(low+1==high)//只有两个点 return dis(p[low],p[high]); if(low+2==high)//只有三个点 return min(dis(p[low],p[high]),min(dis(p[low],p[low+1]),dis(p[low+1],p[high]))); int mid=(low+high)>>1;//求中点即左右子集的分界线 double d=min(closest(low,mid),closest(mid+1,high)); for(i=low,k=0;i<=high;i++)//把x坐标在p[mid].x-d ~ p[mid].x+d范围内的点筛选出来 { if(p[i].x >=p[mid].x-d&&p[i].x<=p[mid].x+d) a[k++]=i;//保存这些点的下标索引 } sort(a,a+k,cmpy);//按y坐标进行升序排序 for(i=0;i<k;i++) { for(j=i+1;j<k;j++) { if(p[a[j]].y-p[a[i]].y>=d)//注意下标索引 break; d=min(d,dis(p[a[i]],p[a[j]])); } } return d;}int main(){ int i,n; while(scanf("%d",&n) &&n!=0)//n个点 { for(i = 0 ; i < n ; ++i) scanf("%lf %lf",&p[i].x,&p[i].y); if(n==1) printf("INFINITY\n"); else { sort(p , p + n , cmpx);//按x坐标进行升序排序 if(closest(0 , n - 1)>10000) printf("INFINITY\n"); else printf("%.4f\n",closest(0 , n - 1));//最近点对间的距离 } } return 0;}
0 0
- UVa 10245 - The Closest Pair Problem
- UVa 10245 - The Closest Pair Problem
- uva 10245 - The Closest Pair Problem
- [uva] 10245The Closest Pair Problem
- UVa 10245 - The Closest Pair Problem
- UVA 10245 - The Closest Pair Problem
- uva 10245 The Closest Pair Problem
- uva 10245 The Closest Pair Problem
- UVa 10245 - The Closest Pair Problem
- UVa 10245 - The Closest Pair Problem
- UVa 10245 - The Closest Pair Problem
- UVA 10245 - The Closest Pair Problem
- UVA - 10245 The Closest Pair Problem
- UVA - 10245 The Closest Pair Problem
- uva 10245 The Closest Pair Problem
- uva 10245 The Closest Pair Problem
- UVA 10245 The Closest Pair Problem【分治】
- UVA 10245 The Closest Pair Problem
- zoj2514题解
- Glib学习(6) 动态数组 Arrays
- PPT怎么插入图案填充效果
- 【MySql性能优化二】利用explain进行查询和分析sql语句
- 第1课:解密SparkStreaming另类实验及SparkStreaming本质解析
- UVA 10245 The Closest Pair Problem
- Http认证模式
- UI控件--自定义SeekBar样式
- hzauoj The Same Color (模拟)
- 哈理工OJ 1179 下山(DP+深搜)
- 关于“Error: "const char *" 类型的实参与 "LPCWSTR"类型的形参不兼容”错误的解决方案
- Android中各种奇葩,难解的Json
- C++虚基类的内存布局(上)
- 飞思卡尔高级驾驶员辅助系统(ADAS)解决方案