hdu 1007 N个点中输出2点的最小距离的一半
2015-10-11 19:29
381 查看
分治法
Sample Input
2
0 0
1 1
2
1 1
1 1
3
-1.5 0
0 0
0 1.5
0
Sample Output
0.71
0.00
0.75
# include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <string> # include <cmath> # include <queue> # include <list> # define LL long long using namespace std ; const double eps = 1e-6; const int MAXN = 100010; const double INF = 1e20; struct Point { double x,y; }; double dist(Point a,Point b) { return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)); } Point p[MAXN]; Point tmpt[MAXN]; bool cmpxy(Point a,Point b) { if(a.x != b.x)return a.x < b.x; else return a.y < b.y; } bool cmpy(Point a,Point b) { return a.y < b.y; } double Closest_Pair(int left,int right) { double d = INF; if(left == right)return d; if(left + 1 == right) return dist(p[left],p[right]); int mid = (left+right)/2; double d1 = Closest_Pair(left,mid); double d2 = Closest_Pair(mid+1,right); d = min(d1,d2); int k = 0; for(int i = left;i <= right;i++) { if(fabs(p[mid].x - p[i].x) <= d) tmpt[k++] = p[i]; } sort(tmpt,tmpt+k,cmpy); for(int i = 0;i <k;i++) { for(int j = i+1;j < k && tmpt[j].y - tmpt[i].y < d;j++) { d = min(d,dist(tmpt[i],tmpt[j])); } } return d; } int main() { //freopen("in.txt","r",stdin) ; int n; while(scanf("%d",&n)==1 && n) { for(int i = 0;i < n;i++) scanf("%lf%lf",&p[i].x,&p[i].y); sort(p,p+n,cmpxy); printf("%.2lf\n",Closest_Pair(0,n-1)/2); } return 0; }View Code
相关文章推荐
- 给Cocos2D视图添加手势支持
- 给Cocos2D视图添加手势支持
- MooseFS之数据存储服务器的安装与配置
- BestCoder Round #59 (div.2)B.Reorder the Books
- ListView异步加载图片--图片缓存和错位问题解决方案
- 排序背包(hdu 3466 & hdu 5501)
- 测试汇总
- Android按钮跳转页面
- 为了和windows一样习惯
- 阿里云CentOS 6.5 设备、执行Docker容器和步骤的方法
- PIC24, dsPIC单片机端C语言serial bootloader和PC端C#语言bootloader串口通信程序
- HDU 1015 Safecracker 解决问题的方法
- Android Design ItemTouchHelper实现酷炫列表的移行和滑动删除效果
- Android中使用pull解析xml文件
- 科研/文献/论文——总架构目录
- cowboy使用restful的例子
- 4.Objective-C 继承
- 顺序串的实现C++封装
- 在LCD上显示USB摄像头图像
- C# 委托 Action<T>