hdoj 1007 临近点对算法模板
2016-07-18 23:04
381 查看
思想 分治
所有点按x坐标排序
每次把整体分成两边求出两边所有点中的最短距离然后求出中间一下片点的最短距离最后合并得总体最短距离
所有点按x坐标排序
每次把整体分成两边求出两边所有点中的最短距离然后求出中间一下片点的最短距离最后合并得总体最短距离
#include<cstdio> #include<vector> #include<algorithm> #include<iostream> #include<cmath> #define LL long long #define pi acos(-1.0) #define maxn 100005 using namespace std; struct node{ double x,y; }no[maxn],no2[maxn]; int n; double min(double a,double b) { return a<b?a:b; } bool cmpX(node a,node b) { return a.x<b.x; } bool cmpY(node a,node b) { return a.y<b.y; } double dis(node a,node b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } double closet(int s,int e) { if(e-s==1) return dis(no[s],no[e]); if(e-s==2) return min(min(dis(no[s],no[s+1]),dis(no[s+1],no[e])),dis(no[s],no[e])); int mid = (s+e)>>1; double ans = min(closet(s,mid),closet(mid+1,e)); int l = 0; //合并 for(int i=s;i<=e;i++) if(no[i].x>no[mid].x-ans&&no[i].x<no[mid].x+ans)no2[l++] = no[i]; sort(no2,no2+l,cmpY); for(int i=0;i<l;i++) for(int j=i+1;j<l;j++) { if(no2[j].y-no2[i].y>=ans)break; ans = min(ans,dis(no2[i],no2[j])); } return ans; } int main() { while(scanf("%d",&n)&&n) { for(int i=0;i<n;i++)scanf("%lf %lf",&no[i].x,&no[i].y); sort(no,no+n,cmpX); printf("%.2f\n",closet(0,n-1)/2.0); } return 0; }
相关文章推荐
- hdu5413 CRB and Roads(topo + bitset)
- NDK配置之Linux环境
- Java烧脑驴游(三)--对象和类
- Hadoop概念学习系列之常见的分布式文件系统(二十六)
- sqlite应用demo
- unity Android csv 数据持久化
- html中的a标签(超链接)的使用
- 使用Notepad++将多行数据合并成一行
- codeforces-327C Magic Five(等比数列求和+快速幂+逆元)
- Speech 模块加载出错
- JHTP小结_第十章_面向对象编程-多态(Polymorphism)
- servlet的三种创建模式
- 正则表达式匹配任意字符(包括换行符)的写法
- 响应式布局研究
- vs2010+qt插件,开发i环境搭建
- iOS Core Graphics封装虚线
- java中的加锁机制-Lock
- iOS socket小结01
- c语言选择法排序
- 创业公司的管理工具集萃