HDU 1007Quoit Design(最近点问题)
最近点问题:二维平面中有n(n很大)个点,求出距离最近的两个点 思路:因为n的值很大,所以暴力和dp都行不通了吧!分治法就挺好的。
将区间一半一半的分开,直到分成只有一个点或两个点的时候!
对于只有两个点的区间,最小值就是这两个点的距离,只有一个点的区间,
最小值就是无穷大。注意还要考虑合并的时候,可能距离最近的两个点,
分别在左右两个不同的区间。对于这种情况的处理如下:
mid=(ld+rd)/2;
ans = min(solve(ld, mid), solve(mid+1, rd));得到两段区间最小值的最小值
从中间向两边寻找,因为我们是按照x坐标排序的,在左区间向左边寻找的时候
如果某一个点的x到中间点x的距离大于ans(否则将这样的点保存),那么这个
点左边的点就不可能在右区间寻找到相应的点满足两个点的距离小于ans的,那么
就结束继续查找(这样算是一种优化) 同理在右区间向右寻找。。。 然后对存储的节点按照y坐标进行从小到大的排序。
枚举每两个点寻找最小的距离
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define MAX 99999999999999.0
using namespace std; struct node{
double x, y;
}nd[], ndx[]; bool cmp(node a, node b){
if(a.x == b.x) return a.y < b.y;
return a.x < b.x;
} bool cmpy(node a, node b){
return a.y < b.y;
} double dist(node a, node b){
return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
} double solve(int ld, int rd){
if(ld == rd) return MAX;
if(ld + == rd) return dist(nd[ld], nd[rd]);
int mid = (ld+rd)/;
double ans = min(solve(ld, mid), solve(mid+, rd));
int len = ;
for(int i = mid; i>=ld; --i)
if(nd[mid].x - nd[i].x <= ans)
ndx[len++] = nd[i];
else break;
for(int i=mid+; i<=rd; ++i)
if(nd[i].x - nd[mid].x <= ans)
ndx[len++] = nd[i];
else break; sort(ndx, ndx+len, cmpy) ;
for(int i=; i<len-; ++i)
for(int j=i+; j<len; ++j)
if(ndx[j].y - ndx[i].y >= ans) break;//这里做一处优化
else ans = min(ans, dist(ndx[i], ndx[j]));
return ans;
} int main(){
int n;
while(scanf("%d", &n) && n){
for(int i=; i<n; ++i)
scanf("%lf%lf", &nd[i].x, &nd[i].y);
sort(nd, nd+n, cmp);
printf("%.2lf\n", solve(, n-)/2.0);
}
return ;
}
HDU 1007Quoit Design(最近点问题)的更多相关文章
- hdu 1007 Quoit Design (最近点对问题)
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 最近点对问题 HDU Quoit Design 1007 分治法
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #i ...
- HDU 1007 Quoit Design最近点对( 分治法)
题意: 给出平面上的n个点,问任意点对之间的最短距离是多少? 思路: 先将所有点按照x坐标排序,用二分法将n个点一分为二个部分,递归下去直到剩下两或一个点.对于一个部分,左右部分的答案分别都知道,那么 ...
- 杭电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(最近点对)
题目连接:hdu_1007_Quoit Design 题意: 给你平面上的一些点,让你找出这些点的最近点对的距离 题解: 采用分治,达到O(nlognlogn)的时间复杂度就能艹过去了 #includ ...
- HDU 1031 Design T-Shirt
http://acm.hdu.edu.cn/showproblem.php?pid=1031 题意 :n个人,每个人对m件衣服打分,每个人对第 i 件衣服的打分要加起来,选取和前 k 高的输出他们的编 ...
- hdu 4631(最近点对,容器)
点击打开链接 题意: 给你一个平面,每次加入一个点,当点数>=2时,求最近点对距离的平方,最后输出所有的平方和. 给你a,b,c x[0]=0;x[i]=(x[i-1]*a+b)%c 如果按照平 ...
- 杭电 HDU 1031 Design T-Shirt
Design T-Shirt Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- HDU 1031.Design T-Shirt【结构体二次排序】【8月21】
Design T-Shirt Problem Description Soon after he decided to design a T-shirt for our Algorithm Board ...
随机推荐
- 排列组合算法(PHP)
用php实现的排列组合算法.使用递归算法,效率低,胜在简单易懂.可对付元素不多的情况. //从$input数组中取$m个数的组合算法 function comb($input, $m) { if($m ...
- MySQL数据导出
1,打开命令行窗口“运行”-->输入CMD 2,进入自己MySQL Server安装目录的bin目录(我的安装目录如下) cd C:\Program Files\MySQL\MySQL Serv ...
- Sql Servicer 复习笔记(1) 存储过程分布
第一步:创建表 declare @countInt int declare @age int ) begin ),@countInt), @age,'中国北京') ; ; ) begin ; end ...
- Python成长笔记 - 基础篇 (十二)
本节内容 ORM介绍 sqlalchemy安装 sqlalchemy基本使用 多外键关联 多对多关系 表结构设计作业 主题:学员管理系统 需求: 用户角色,讲师\学员, 用户登陆后根据角色不同,能做的 ...
- Autolayout(VFL)
Autolayout(VFL) 1.NSLayoutConstraint + (NSArray *)constraintsWithVisualFormat:(NSString *)format opt ...
- C语言-数据类型
数据类型 -基本数据类型 --char 字符型 --int 整型 --浮点型 ---float ---double -指针类型 --void* -空类型 -构架类型 --数组[] --结构体 str ...
- 从头安装及配置PL/SQL Developer
原文地址:http://lightguide.blog.51cto.com/3121539/1140588 因为自己安装及配置中走了不少弯路,搜索网上的文档大部分没有包含oralce client的安 ...
- poj 1195 - Mobile phones(树状数组)
二维的树状数组,,, 记得矩阵的求和运算要想好在写.... 代码如下: #include <cstdio> #include <cstdlib> #include <cm ...
- (转)Linux服务器调优
Linux内核参数 http://space.itpub.net/17283404/viewspace-694350 net.ipv4.tcp_syncookies = 1 表示开启SYN Cooki ...
- linux下用rpm包安装默认配置
rpm安装默认目录:数据文件:/var/lib/mysql/配置文件模板:/usr/share/mysqlmysql客户端工具目录:/usr/bin日志目录:/var/log/pid,sock文件目录 ...