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 ...
随机推荐
- Runlevel in Linux
运行级别(Runlevel)指的是Unix或者Linux等类Unix操作系统下不同的运行模式.运行级别通常分为7等,分别是从0到6,但如果必要的话也可以更多. 例如在大多数Linux操作系统下一共有如 ...
- Python:os模块 time模块
1.os os.popen('cmd').read() 执行某个系统命令,:输出命令结果 os.getcwd() 获取当前操作目录 os.makedirs(r"C:\a\b\c" ...
- chrome最强大的浏览器插件推荐,只要你会用其他的插件你可以删除了
我们在学习和工作中经常会需要用到各种各样不同需求的插件,结果chrome插件越装越多,chrome浏览器也越来越慢!有时候链我们自己都懵圈了,一时间都想不起来这个插件是干什么用的.更可气的是,很多时候 ...
- LeetCode(55)Jump Game
题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...
- 集训第六周 O题
Description Gerald got a very curious hexagon for his birthday. The boy found out that all the angle ...
- 九度oj 题目1070:今年的第几天?
题目1070:今年的第几天? 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6832 解决:3779 题目描述: 输入年.月.日,计算该天是本年的第几天. 输入: 包括三个整数年(1< ...
- MySQL-Transfer2.2发布
http://dinglin.iteye.com/blog/1888640 Transfer 2.2发布.下载地址 版本说明 1. 基于版本 Percona-5.5.31 ,简单用法是先安装好官方或 ...
- 前端开发:JavaScript---ECMAScript
JavaScript:JavaScript是一种web前端的描述语言,也是一种基于对象(object)和事件驱动(Event Driven)的脚本语言.它运行在客户端从而减轻服务器的负担. js是一种 ...
- OpenCV在Linux(Fedora)下搭建开发环境简述
盼望了好久的Fedora21终于发行了.先来晒一张图: 默认桌面还是那么简洁: 好了,废话少说.来看看在Fedora下搭建opencv开发环境,因为我已经搭建好了(过程比较艰辛) 先注明参考文章,感谢 ...
- 从零开始写STL-容器-双端队列
从零开始写STL-容器-双端队列 什么是双端队列?在介绍vector源码,我们发现在vector前端插入元素往往会引起大量元素的重新分配,双端队列(deque)就是为了解决这一问题,双端队列中在首端和 ...