ZOJ2107 给定10^5个点,求距离最近的点对的距离。 O(n^2)的算法是显而易见的。

可以通过分治优化到O(nlogn)

代码很简单

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std; const double eps=1e-9; int cmp(double x)
{
if(fabs(x)<eps)return 0;
if(x>0)return 1;
else return -1;
} const double pi=acos(-1.0); inline double sqr(double x)
{
return x*x;
} struct point
{
double x,y;
point (){}
point (double a,double b):x(a),y(b){}
void input()
{
scanf("%lf%lf",&x,&y);
}
friend point operator +(const point &a,const point &b)
{
return point(a.x+b.x,a.y+b.y);
}
friend point operator -(const point &a,const point &b)
{
return point(a.x-b.x,a.y-b.y);
}
friend bool operator ==(const point &a,const point &b)
{
return cmp(a.x-b.x)==0&&cmp(a.y-b.y)==0;
}
friend point operator *(const point &a,const double &b)
{
return point(a.x*b,a.y*b);
}
friend point operator*(const double &a,const point &b)
{
return point(a*b.x,a*b.y);
}
friend point operator /(const point &a,const double &b)
{
return point(a.x/b,a.y/b);
}
double norm()
{
return sqrt(sqr(x)+sqr(y));
}
}; const int maxn=100000+10;
point a[maxn];
int n,s[maxn];
bool cmpx(int i,int j)
{
return cmp(a[i].x-a[j].x)<0;
} bool cmpy(int i,int j)
{
return cmp(a[i].y-a[j].y)<0;
} double min_dist(point a[],int s[],int l,int r)
{
double ans=1e100;
if(r-l<20)
{
for(int q=l;q<r;q++)
for(int w=q+1;w<r;w++)ans=min(ans,(a[s[q]]-a[s[w]]).norm());
return ans;
}
int tl,tr,m=(l+r)/2;
ans=min(min_dist(a,s,l,m),min_dist(a,s,m,r));
for(tl=l;a[s[tl]].x<a[s[m]].x-ans;tl++);
for(tr=r-1;a[s[tr]].x>a[s[m]].x+ans;tr--);
sort(s+tl,s+tr,cmpy);
for(int q=tl;q<tr;q++)
for(int w=q+1;w<min(tr,q+5);w++)
ans=min(ans,(a[s[q]]-a[s[w]]).norm());
sort(s+tl,s+tr,cmpx);
return ans;
} double Min_Dist(point a[],int s[],int n)
{
for(int i=0;i<n;++i)s[i]=i;
sort(s,s+n,cmpx);
return min_dist(a,s,0,n);
} int main()
{freopen("t.txt","r",stdin);
int n;
while(scanf("%d",&n)&&n!=0)
{
for(int i=0;i<n;i++)
a[i].input();
printf("%.2lf\n",Min_Dist(a,s,n)/2.);
}
return 0;
}

  

ZOJ2107 Quoit Design 最近点对的更多相关文章

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

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

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

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

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

    题意: 给出平面上的n个点,问任意点对之间的最短距离是多少? 思路: 先将所有点按照x坐标排序,用二分法将n个点一分为二个部分,递归下去直到剩下两或一个点.对于一个部分,左右部分的答案分别都知道,那么 ...

  4. Quoit Design(最近点对+分治)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 Quoit Design Time Limit: 10000/5000 MS (Java/Oth ...

  5. Quoit Design(hdu1007)最近点对问题。模版哦!

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

  6. ZOJ 2017 Quoit Design 经典分治!!! 最近点对问题

    Quoit Design Time Limit: 5 Seconds      Memory Limit: 32768 KB Have you ever played quoit in a playg ...

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

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

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

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

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

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

随机推荐

  1. MongoDB数据库的安装

    首先就是MongoDB的下载,可以去MongoDB官网进行下载,https://www.mongodb.com/download-center/community,也可以通过百度网盘直接下载, 链接: ...

  2. python 博客开发之散乱笔记

    博客开发之旅: # 回滚,数据存储失败时,还原修改操作 from django.db import transaction with transaction.atomic(): do... ... # ...

  3. 2017 计蒜之道 初赛 第一场 A 阿里的新游戏

    题链:https://nanti.jisuanke.com/t/15499 这题观察图纸可知成三线段上的相邻点之间的距离有1,2,3三种情况的,同时要成线段必然是同横坐标或者纵坐标,然后我们排除掉穿过 ...

  4. [bzoj1078][SCOI2008][斜堆] (贪心)

    Description 斜堆(skew heap)是一种常用的数据结构.它也是二叉树,且满足与二叉堆相同的堆性质:每个非根结点的值都比它父亲大.因此在整棵斜堆中,根的值最小.但斜堆不必是平衡的,每个结 ...

  5. PHP读取mysql中的数据

    <!DOCTYPE HTML> <html> <head> <title> PHP动态读取mysql中的数据 </title> <me ...

  6. [luoguP2146] 软件包管理器(树链剖分)

    传送门 看着很吓人,其实就是个树链剖分模板. 可支持操作: 1.将节点 x 到 根 的路径上的值都变成 1 2.将以节点 x 为根的子树的值都变成 0 1A爽~ ——代码 #include <c ...

  7. android开发里跳过的坑——图片文件上传失败

    使用的apache的httpclient的jar包,做的http图片上传,上传时,服务器总返文件格式不对.后来发现,是由于在创建FileBody时,使用了默认的ContentType引起的.所以服务器 ...

  8. msp430入门编程10

    msp430中C语言操作端口I/O10 msp430中C语言的模块化头文件及实现11 msp430中C语言的模块化头文件及库文件12 msp430入门学习 msp430入门编程

  9. JavaScript面向对象实现

    JavaScript面向对象实现 一:面向对象三大特征 继承,封装,多态! 二:JavaScript自定义对象  创建对象的方式: 方式1,对象初始化器方式: <script type=&quo ...

  10. Evaluate Reverse Polish Notation(逆波兰式)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...