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 ...
随机推荐
- 用ffmpeg切割音频文件
ffmpeg -i audio.wav -f segment -segment_time -c copy audio%02d.wav "-segment_time 60" 表示每6 ...
- 树莓派 - 驱动hello
树莓派上Linux驱动,从hello world 开始 ... hello.c #include <linux/init.h> #include <linux/module.h> ...
- Angular网络请求的封装
很多时候,我很喜欢angular的编码风格,特别是angular支持typescript之后,完整的生命周期,完美的钩子函数,都是别的语言所无法替代的.这里我来说说我自己的网络请求封装,某种意义上来说 ...
- ORM之连表操作
ORM之连表操作 -----------------------------连表的正向操作------------------------- 在models.py中创建两张表UserType和User ...
- 【转载】什么是Zero-Copy
转载:https://blog.csdn.net/u013256816/article/details/52589524 概述 考虑这样一种常用的情形:你需要将静态内容(类似图片.文件)展示给用户.那 ...
- Python中接收用户的输入
一.如何去接收用户的输入?使用函数 input() 函数 input() 让程序暂停运行,等待用户输入一些文本,获取用户的输入后,Python将其存储到一个变量中,以方便后期使用. name = in ...
- 当java web项目部署到服务器上时,无法将图片等媒体文件保存到服务器的最终奥义
今天,我在部署web项目至云服务器上(Tomcat8.0)时,突然发现我的应用,无法上传图片,视频等多媒体文件了,一再检查自己的代码逻辑没有问题之后,逐一排查,首先想到的就是看一下控制台打印的日志,日 ...
- python 深浅拷贝&集合
一.深浅拷贝 1.浅拷贝,只会拷贝第一层 s = [1, 'ss', '小可爱'] s1 = s.copy() print(s1) >>> [1, 'ss', '小可爱'] s = ...
- mysql 判断索引是否存在,存在则删除再创建索引(分表) 存储过程
1.分表5数据量大,执行所有分表修改,不包括5 CREATE PROCEDURE deleteIndex()BEGINDECLARE corpId CHAR (16);DECLARE flag INT ...
- 【ZJOI2017 Round1练习&&BZOJ5353】D7T2 guess(费用流)
题意: 思路: ..]of longint; pre:..,..]of longint; inq:..]of boolean; q:..]of longint; n,m,i,j,ans,tot,sou ...