hdu 1007 Quoit Design(平面最近点对)
题意:求平面最近点对之间的距离
解:首先可以想到枚举的方法,枚举i,枚举j算点i和点j之间的距离,时间复杂度O(n2).
如果采用分治的思想,如果我们知道左半边点对答案d1,和右半边点的答案d2,如何求跨两边点之间的答案呢?显然只用枚举中线两边d=min(d1,d2)范围的点,并且每个点都只需要枚举上下范围在d以内的点,显然这样的点不会很多。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <map>
using namespace std;
typedef long long ll;
const double inf=1e20;
const int maxn=; struct Point{
double x, y;
}point[maxn]; int n, mpt[maxn]; //以x为基准排序
bool cmpxy(const Point &a,const Point &b){
if(a.x!=b.x)
return a.x<b.x;
return a.y<b.y;
} bool cmpy(const int &a,const int &b){
return point[a].y<point[b].y;
} double min(double a,double b){
return a<b?a:b;
} double dis(int i,int j){
return sqrt((point[i].x-point[j].x)*(point[i].x-point[j].x) + (point[i].y - point[j].y)*(point[i].y - point[j].y));
} double Closest_Pair(int left, int right){
double d=inf;
if(left==right)
return d;
if(left+==right)
return dis(left,right);
int mid=(left+right)>>;
double d1=Closest_Pair(left,mid);
double d2=Closest_Pair(mid+,right);
d=min(d1,d2);
int i,j,k=;
//分离出宽度为d的区间
for(i=left;i<=right;i++){
if(fabs(point[mid].x-point[i].x)<=d)
mpt[k++]=i;
}
sort(mpt,mpt+k,cmpy);
//线性扫描
for(i=;i<k;i++){
for(j=i+;j<k&&point[mpt[j]].y-point[mpt[i]].y<d; j++){
double d3=dis(mpt[i],mpt[j]);
if(d>d3)d=d3;
}
}
return d;
} int main(){
while(scanf("%d",&n)!=EOF){
if(n==)break;
for(int i=;i<n;i++)
scanf("%lf%lf",&point[i].x,&point[i].y);
sort(point,point+n,cmpxy);
printf("%.2lf\n",Closest_Pair(,n-)/);
}
return ;
}
hdu 1007 Quoit Design(平面最近点对)的更多相关文章
- HDU 1007 Quoit Design 平面内最近点对
http://acm.hdu.edu.cn/showproblem.php?pid=1007 上半年在人人上看到过这个题,当时就知道用分治但是没有仔细想... 今年多校又出了这个...于是学习了一下平 ...
- HDU 1007 Quoit Design | 平面分治
暂鸽 #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #d ...
- 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 ...
- 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) To ...
- HDU 1007 Quoit Design
传送门 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Des ...
- HDU 1007 Quoit Design(计算几何の最近点对)
Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...
随机推荐
- 【C++】C++程序加载lib静态库
使用Visual Studio 编写C++程序有几种配置lib的方法,以下是在代码中加载lib文件的方法: 在项目所在目录下创建文件夹lib,将lib文件此路径下,包括Debug和Release两种模 ...
- scala中符号的意思
1. => 定义函数, xxx => yyy 左边是函数变量,右边是函数返回值 2. <- 遍历中的<- 将变量赋给索引 for( i <- arrs ) 3. -> ...
- Mysql索引优化简单介绍
一.关于MySQL联合索引 总结记录一下关于在MySQL中使用联合索引的注意事项. 如:索引包含表中每一行的last_name.first_name和dob列,即key(last_name, firs ...
- 开源堡垒机jumpserver的配置和使用
开源跳板机jumpserver配置和使用 http://docs.jumpserver.org/zh/docs/quick_start.html#id9 系统设置 基本设置 # 修改url 的&quo ...
- chatrr lsatrr
PS:有时候你发现用root权限都不能修改某个文件,大部分原因是曾经用chattr命令锁定该文件了.chattr命令的作用很大,其中一些功能是由Linux内核版本来支持的,不过现在生产绝大部分跑的li ...
- docker部署带mysql数据库连接的.netcore程序
docker部署带mysql数据库连接的程序和部署普通的程序完全一致 数据库可以是物理机删的mysql,同时也可以是docker里的mysql. 如果是docker中的mysql,配置连接字符串和物理 ...
- 量化投资学习笔记27——《Python机器学习应用》课程笔记01
北京理工大学在线课程: http://www.icourse163.org/course/BIT-1001872001 机器学习分类 监督学习 无监督学习 半监督学习 强化学习 深度学习 Scikit ...
- 复制表结构创建分表 再设置自增ID
CREATE TABLE table_name1 LIKE table_name ALTER TABLE test AUTO_INCREMENT=x
- View Binding初探
参考翻译:https://developer.android.google.cn/topic/libraries/view-binding View Binding是一项功能,使您可以更轻松地编写与视 ...
- VUE中使用XLSX实现导出excel表格
简介 项目中经常会用导出数据的场景,这里介绍 VUE 中如何使用插件 xlsx 导出数据 安装 ## 1.使用 npm 或 yarn 安装依赖(三个依赖) npm install -S file-sa ...