最近点对问题 HDU Quoit Design 1007 分治法
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<string>
#include<queue>
using namespace std;
const int maxn = ;
const long long INF = 0xfffffff;
struct Point
{
double x, y;
}P[maxn], Temp[maxn];
bool cmp(Point a, Point b)
{
if(a.x != b.x)
return a.x < b.x;
return a.y < b.y;
}
bool cmpy(Point a, Point b)
{
return a.y < b.y;
}
double dis(Point a, Point b)
{
return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) );
}
double Colosest(int L,int R)
{
if(L == R)
return INF;
if(L + == R)
return dis(P[L],P[R]);
int Mid = (L + R) / ;
double d1 = Colosest(L, Mid);
double d2 = Colosest(Mid + , R);
double d = min(d1, d2);
//递归找左右两侧的点,最小距离
int k = ; for(int i = Mid; i >= L; i--)
{
if( P[Mid].x - P[i].x < d )
Temp[k++] = P[i];
else
break;
} for(int j = Mid + ; j <= R; j++)
{
if(P[j].x - P[Mid].x < d)
Temp[k++] = P[j];
else
break;
}
//找出在中间点的可能点对 然后排序
sort(Temp,Temp+k,cmpy); for(int i=; i<k; i++)
{
/*需要一个 Temp[j].y - Temp[i].y < d 优化, 不然会超时*/
for(int j=i+; j < k && Temp[j].y - Temp[i].y < d ; j++)
{
double d3 = dis(Temp[j], Temp[i]);
if(d3 < d)
d = d3;
}
}
return d;
}
int main()
{
int n;
while(scanf("%d",&n) ,n)
{
for(int i=; i<n; i++)
scanf("%lf%lf",&P[i].x,&P[i].y);
sort(P,P+n,cmp);//先排序
printf("%.2lf\n", Colosest(,n-)/ );
}
return ;
}
最近点对问题 HDU Quoit Design 1007 分治法的更多相关文章
- 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(计算几何の最近点对)
Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...
- HDU ACM 1007 Quoit Design 分而治之的方法,最近点
意甲冠军:给n坐标点.半一对点之间的距离所需的距离最近. 分析:分而治之的方法,最近点. #include<iostream> #include<algorithm> #inc ...
- HDU 1007 Quoit Design(二分+浮点数精度控制)
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- 杭电OJ——1007 Quoit Design(最近点对问题)
Quoit Design Problem Description Have you ever played quoit in a playground? Quoit is a game in whic ...
- 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design
题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...
随机推荐
- [Angular 2] NgNonBindable
If you want to print someting like {{content}} on the html, using ng-non-bindable directive: <div ...
- linux下查看磁盘空间
如果要查看磁盘还剩多少空间,当然是用df的命令了. [root@localhost ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/sda2 ...
- 亲测 安装 mysql5.5 64位
1.选择 customn 更改安装目录2.选择 develop 模式,默认,占用内存较小. server machine 中等内存 dedicate mysql server machine 专用服务 ...
- Android(java)学习笔记219:开发一个多界面的应用程序之两种意图
1.两种意图: (1)显式意图: 在代码里面用intent设置要开启Activity的字节码.class文件: (2)隐式意图: Android(java)学习笔记218:开发一个多界面的应用程序之人 ...
- jquery ajax 提交表单(file && input)
用到的插件 jquery.js jquery.form.js[http://malsup.github.io/jquery.form.js] 提交页面 <form enctype="m ...
- Shell 脚本模拟 milter 实现黑白名单及关键词过滤
程序执行流程:1. 开始接受邮件.2. 检查发件人是否在黑名单内,如果是拒绝接受;否则继续3. 检查发件人是否在白名单内,如果是接收邮件;否则继续4. 对邮件进行关键字过滤,如果邮件中包含被过滤的关键 ...
- C#解leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- C# 自定义线程修改UI(一)
在Wpf中界面显示数据的修改,都是通过UI线程完成,如果尝试从其他线程中直接修改控件的值回抛出异常,“调用线程无法访问此对象,因为另一个线程拥有该对象”. 例如:http://www.cnblogs. ...
- (转)DedeCms Runphp 标签中调用其他变量的方法
DedeCms Runphp 标签中调用其他变量的方法 我们都知道,在DedeCMS中是可以使用PHP的,常见的方法就是if else了,例如模板制作中,我们需要对来源和作者进行判断,如果为空,则提示 ...
- 自定义android精美聊天界面
编写精美聊天界面,那就肯定要有收到的消息和发送的消息. 首先还是编写主界面,修改activity_chat.xml中的代码,如下所示: <?xml version="1.0" ...