ZOJ2107 Quoit Design 最近点对
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 最近点对的更多相关文章
- 杭电OJ——1007 Quoit Design(最近点对问题)
Quoit Design Problem Description Have you ever played quoit in a playground? Quoit is a game in whic ...
- hdu 1007 Quoit Design (最近点对问题)
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1007 Quoit Design最近点对( 分治法)
题意: 给出平面上的n个点,问任意点对之间的最短距离是多少? 思路: 先将所有点按照x坐标排序,用二分法将n个点一分为二个部分,递归下去直到剩下两或一个点.对于一个部分,左右部分的答案分别都知道,那么 ...
- Quoit Design(最近点对+分治)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 Quoit Design Time Limit: 10000/5000 MS (Java/Oth ...
- Quoit Design(hdu1007)最近点对问题。模版哦!
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- ZOJ 2017 Quoit Design 经典分治!!! 最近点对问题
Quoit Design Time Limit: 5 Seconds Memory Limit: 32768 KB Have you ever played quoit in a playg ...
- HDU 1007 Quoit Design(经典最近点对问题)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1007 Quoit Design Time Limit: 10000/5000 MS (Java/Oth ...
- HDU 1007 Quoit Design【计算几何/分治/最近点对】
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1007 Quoit Design 分治求最近点对
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
随机推荐
- Buffer.isBuffer()详解
Buffer.isBuffer(obj) obj {Object} 返回:{Boolean} 如果 obj 是一个 Buffer 则返回 true.
- 89-Relative Vigor Index 相对活力指数指标.(2015.7.4)
Relative Vigor Index 相对活力指数指标 ~计算: RVI = (CLOSE-OPEN)/(HIGH-LOW) RVIsig=SMA(RVI,N) ~思想: 牛市中,收盘>开盘 ...
- <vue>…… v-if 与 v-show ……//
#v-if 用法: 根据表达式的值的真假条件渲染元素.在切换时元素及它的数据绑定 / 组件被销毁并重建.如果元素是 <template> ,将提出它的内容作为条件块. 当条件变化时该指令触 ...
- Java异常使用要点记录
近期有离职的想法,奈何简历过于寒碜,技术懂的少,基础也薄弱,想要提升自己却不知从哪里入手.在郁闷一段时间后偶然间看到一篇博客,博主说出了许多人的心声,同时也指出了切入点,的确基础才是重点,是时候沉迷学 ...
- 图解使用IDEA创建第一个Java程序HelloWorld
前几次给大家分享了怎么在自己的电脑上配置 java 环境,准备工作做好了,我们就要开始我们真正的编码学习了.下面介绍使用 IDEA 创建我们的第一个 HelloWorld 程序. 1.打开 IDEA, ...
- 【转】Asp.net MVC Comet推送
原文链接:http://www.cnblogs.com/kissdodog/p/4283485.html 一.简介 在Asp.net MVC实现的Comet推送的原理很简单. 服务器端:接收到服务器发 ...
- BNUOJ 5363 Machine Schedule
Machine Schedule Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Origin ...
- ORACLE RAC with NFS install
第一步:建立第一台节点机1).拷贝一台虚拟机做第一个节点,增加host-only的网卡 kudzu工具识别网卡(两都host-only是可以的).然后以下步骤修改相应的配置.etc/hosts内容如下 ...
- 无法打开物理文件 "X.mdf"。操作系统错误 5:"5(拒绝访问。)"。 (Microsoft SQL Server,错误: 5120)解决
环境 SQLServer 2008 R2 问题 附加数据库出现“无法打开物理文件 "X.mdf".操作系统错误 5:"5(拒绝访问.)". (Microsoft ...
- POJ训练计划
POJ训练计划 Step1-500题 UVaOJ+算法竞赛入门经典+挑战编程+USACO 请见:http://acm.sdut.edu.cn/bbs/read.php?tid=5321 一.POJ训练 ...