【poj 3714 / hdu 1007 /uva 10245】经典题 平面上最近的点对
来源:互联网 发布:云播软件 编辑:程序博客网 时间:2024/06/05 14:24
【问题描述】
给定平面上n个点,找出其中的一对点的距离,使得在这n个点的所有点对中,该距离为所有点对中最小的。
【输入格式】
第一行:n;
接下来n行:每行两个实数:x y,表示一个点的行坐标和列坐标,中间用一个空格隔开。
【输出格式】
仅一行,一个实数,表示最短距离,精确到小数点后面4位。
【输入样例】
3
1 1
1 2
2 2
【输出样例】
1.0000
【数据范围】
对于50%的数据有:2 ≤ n ≤ 5000
对于100%的数据有:1 ≤ n ≤ 100000 , 0 ≤ x,y ≤ 1000000000
【来源】
poj 3714 / hdu 1007 /uva 10245
#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#define maxn 100005#define inf 1111111111using namespace std;int n;struct data{ int x,y;}p[maxn],c[maxn];bool cmpx(data a,data b){ return a.x<b.x;}bool cmpy(data a,data b){ return a.y<b.y;}double getd(int i,int j){ double a=p[i].x-p[j].x; double b=p[i].y-p[j].y; return sqrt(a*a+b*b);}double getd2(int i,int j){ double a=c[i].x-c[j].x; double b=c[i].y-c[j].y; return sqrt(a*a+b*b);}double calc(int x,int y){ if(x>=y)return inf; if(x==y-1)return getd(x,y); int m=(x+y)/2; double dl=calc(x,m); double dr=calc(m+1,y); double d=min(dl,dr); int k=0; for(int i=x;i<=y;i++) if(abs(p[i].x-p[m].x)<d)c[++k]=p[i]; double t=inf; sort(c+1,c+k+1,cmpy); for(int i=1;i<k;i++) for(int j=i+1;j<=k;j++){ if(c[j].y-c[i].y>=d)break; t=min(t,getd2(i,j)); } return min(t,d);}int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y); sort(p+1,p+n+1,cmpx); double ans=calc(1,n); printf("%.4lf",ans); return 0;}// 呵呵,就是求距离两种数组C,P不一样…卡一年 //这尼玛都能超时…… 改了sort的位置就不超了……
0 0
- 【poj 3714 / hdu 1007 /uva 10245】经典题 平面上最近的点对
- 平面上最近点对的距离 POJ 3714
- POJ 3714 Raid 平面最近点对
- POJ 3714(Raid-平面最近点对)
- hdu 1007 Quoit Design 平面上的最近点对 分治法+鸽笼原理
- hdu 1007 平面最近点对 分治
- 平面上最近点对
- 从平面上最近的点对,谈谈分治算法
- poj 3714 Raid(平面最近点对)
- poj 3714 Raid 分治法求平面最近点对
- 平面最近点对 ZOJ 2107 POJ 3714
- POJ 3714 Raid(平面最近点对)
- hdu 1007 Quoit Design(平面最近点对)
- HDU 1007 Quoit Design(计算几何 平面最近点对)
- hdu 1007_平面最近点对模板
- HDU 1007 平面最近点对(计算集几何)
- hdu 1007 Quoit Design 平面最近点对
- 平面上求最近点对问题
- IOS 开发过程中问题汇总
- 项目之JSP标签定义时URI报错的问题(产品追溯系统)2016-7-29
- java??List?LinkedList?ArrayList ??
- IOS 个人苹果开发者账号申请流程
- OSG测试gl.h编译出错
- 【poj 3714 / hdu 1007 /uva 10245】经典题 平面上最近的点对
- Oracle连接类型
- 用户界面框架jQuery EasyUI示例大全之Tree
- jQuery动态添加.active属性实现按钮点击样式
- 【人脸检测】“人脸训练代码”项目笔记(3)——代码结构分析
- 前端的小玩意(7)——自动给每个字下方加着重号
- 区别:Thread.currentThread().getContextClassLoader() and Class.getClassLoader()
- 支付宝移动支付开发错误集锦
- AcEdJig类