意甲冠军:给n坐标点。半一对点之间的距离所需的距离最近。

分析:分而治之的方法,最近点。

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std; #define N 100005 double min(double a,double b)
{
return a<b? a:b;
} struct POINT
{
double x,y;
};
POINT point[N],*px[N],*py[N]; double dis(const POINT* p1,const POINT* p2)
{
return sqrt(pow(p1->x-p2->x,2.0)+pow(p1->y-p2->y,2.0));
} bool cmpx(const POINT* p1,const POINT* p2)
{
return p1->x<p2->x;
} bool cmpy(const POINT* p1,const POINT* p2)
{
return p1->y<p2->y;
} double core(int s,int e) //分治法求最小点对核心代码
{
int mid,i,j,cnt;
double ans; if(s+1==e) //仅仅有两个点的情况
return dis(px[s],px[e]);
if(s+2==e) //仅仅有三个点的情况
return min(dis(px[s],px[s+1]),min(dis(px[s+1],px[e]),dis(px[s],px[e])));
mid=(s+e)>>1;
ans=min(core(s,mid),core(mid+1,e)); //递归求解
for(cnt=0,i=s;i<=e;i++) //把x坐标在px[mid].x-ans~px[mid].x+ans范围内的点取出来
if(px[i]->x>=px[mid]->x-ans && px[i]->x<=px[mid]->x+ans)
py[cnt++]=px[i];
sort(py,py+cnt,cmpy); //按y值排序
for(i=0;i<cnt;i++)
for(j=i+1;j<cnt;j++)
{
if(py[j]->y-py[i]->y>=ans)
break;
ans=min(ans,dis(py[i],py[j]));
}
return ans;
} int main()
{
int i,n; while(scanf("%d",&n)!=EOF && n)
{
for(i=0;i<n;i++)
{
scanf("%lf%lf",&point[i].x,&point[i].y);
px[i]=&point[i];
}
sort(px,px+n,cmpx); //按x坐标排序
printf("%.2lf\n",core(0,n-1)/2.0);
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

HDU ACM 1007 Quoit Design 分而治之的方法,最近点的更多相关文章

  1. HDU 1007 Quoit Design【计算几何/分治/最近点对】

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. hdu 1007 Quoit Design (经典分治 求最近点对)

    Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...

  3. hdu 1007 Quoit Design(分治法求最近点对)

    大致题意:给N个点,求最近点对的距离 d :输出:r = d/2. // Time 2093 ms; Memory 1812 K #include<iostream> #include&l ...

  4. HDU 1007 Quoit Design(经典最近点对问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1007 Quoit Design Time Limit: 10000/5000 MS (Java/Oth ...

  5. HDU 1007 Quoit Design(二分+浮点数精度控制)

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  6. hdu 1007 Quoit Design (最近点对问题)

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  7. hdu 1007 Quoit Design 分治求最近点对

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. 杭电OJ——1007 Quoit Design(最近点对问题)

    Quoit Design Problem Description Have you ever played quoit in a playground? Quoit is a game in whic ...

  9. poj 1007 Quoit Design(分治)

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

随机推荐

  1. Java获得正则表达式

    t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,wid ...

  2. 安卓的sqlite增删改

    基于安卓的sqlite增删改,笔记学习: 1.使用LinearLayout 布局生成,增删改的页面如图 代码布局如下: <LinearLayout xmlns:android="htt ...

  3. JavaScript向select下拉框中加入和删除元素

    JavaScript向select下拉框中加入和删除元素 1.说明 a   利用append()方法向下拉框中加入元素 b   利用remove()方法移除下拉框中最后一个元素 2.设计源代码 < ...

  4. Cocos2d-x实现简单的翻牌效果

    触发器互联网影响找了很多.有自己的点重写一个复杂的sprite类来实现.简单的操作来对引擎的使用CCOrbitCamera实现,但是,也存在一些问题,后变反了. 我在用的仅仅是一个简单的翻牌效果,点击 ...

  5. HDU--3081--Marriage Match II--最大匹配,匈牙利算法

    Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. 乐在其中设计模式(C#) - 桥接模式(Bridge Pattern)

    原文:乐在其中设计模式(C#) - 桥接模式(Bridge Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 桥接模式(Bridge Pattern) 作者:webabcd 介绍 ...

  7. Netty+Tomcat热部署端口占用解决办法(转)

    在eclipse使用maven deploy (tomcat:deploy) 热部署netty项目 ,项目启动的时候会报错端口被占用. java.net.BindException: Address  ...

  8. eclipse在maven项目交付svn忽略简介

    文章来源:http://blog.csdn.net/chaijunkun/article/details/34805385,转载请注明. 不时因为它将有关鲍恩梳理,它会做出相应的内容不变.文. ecl ...

  9. NVIDIA+关联2015写学校招收评论(嵌入式方向,上海)

    我没有写很长一段时间Blog中的一个,在过去的几个月中还没有看到太多的生长技术,来来回回一直在做的事情,要毕业找工作,但发现并没有冷静下来,准备过.这不是让人觉得暂时补习班是凡人啊. 本科不试试.那你 ...

  10. Oracle Applications Multiple Organizations Access Control for Custom Code

    档 ID 420787.1 White Paper Oracle Applications Multiple Organizations Access Control for Custom Code ...