最近点问题:二维平面中有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(最近点问题)的更多相关文章

  1. hdu 1007 Quoit Design (最近点对问题)

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. 最近点对问题 HDU Quoit Design 1007 分治法

    #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #i ...

  3. HDU 1007 Quoit Design最近点对( 分治法)

    题意: 给出平面上的n个点,问任意点对之间的最短距离是多少? 思路: 先将所有点按照x坐标排序,用二分法将n个点一分为二个部分,递归下去直到剩下两或一个点.对于一个部分,左右部分的答案分别都知道,那么 ...

  4. 杭电OJ——1007 Quoit Design(最近点对问题)

    Quoit Design Problem Description Have you ever played quoit in a playground? Quoit is a game in whic ...

  5. hdu_1007_Quoit Design(最近点对)

    题目连接:hdu_1007_Quoit Design 题意: 给你平面上的一些点,让你找出这些点的最近点对的距离 题解: 采用分治,达到O(nlognlogn)的时间复杂度就能艹过去了 #includ ...

  6. HDU 1031 Design T-Shirt

    http://acm.hdu.edu.cn/showproblem.php?pid=1031 题意 :n个人,每个人对m件衣服打分,每个人对第 i 件衣服的打分要加起来,选取和前 k 高的输出他们的编 ...

  7. hdu 4631(最近点对,容器)

    点击打开链接 题意: 给你一个平面,每次加入一个点,当点数>=2时,求最近点对距离的平方,最后输出所有的平方和. 给你a,b,c x[0]=0;x[i]=(x[i-1]*a+b)%c 如果按照平 ...

  8. 杭电 HDU 1031 Design T-Shirt

    Design T-Shirt Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  9. 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 ...

随机推荐

  1. 我的Sharepoint表单使用

    采用客户端验证和后台异步验证.

  2. C# final project

    Problem Statement You are tasked with developing a task manager. The task manager will allow people ...

  3. [Xpand] Error 1 Invalid option '6' for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default

    原因是用的vs2015 默认用了c#6 ,但是在没安装.net 4.6 环境下编译失败. 解决办法很简单,修改 6 为 5 做降级就可以了. 1.nuget install DotNetCompile ...

  4. Texture Atlas

    Texture atlas [1][2] is a technique to group smaller textures into a larger texture. This decreases ...

  5. eclipse中输入中文为繁体

    http://blog.163.com/guomaolin_gavin/blog/static/199618307201218104452930/ eclipse中输入中文为繁体! 2012-02-0 ...

  6. Objective-C 关联

    在项目开发中,经常会使用到关联,就是将两个实例对象绑定,使得其中一个实例对象成为另一个实例对象的一部分.关联特性在mac os 10.6 及ios 3.1以上才可以使用. 关联的使用是基于关键字来实现 ...

  7. 几个非常有用的js小函数

    function $(v){ if(typeof v==="function"){ window.onload=v; }else if(typeof v==="strin ...

  8. Happy Programming Contest(ZOJ3703)(01背包+路径储存)

    Happy Programming Contest  ZOJ3703 老实说:题目意思没看懂...(希望路过的大神指点) 最后那个the total penalty time是什么意思啊!!! 还是学 ...

  9. 我的ORM之十-- MyOql实现原理

    我的ORM索引 ORM的功能 两个基本功能 翻译:对象查询转化为SQL 映射:把查询结果转化为实体 更多功能 如果仅仅实现这两个功能,那这两个是可以独立的,实现起来也就很简单了.但MyOql的目标还有 ...

  10. angular中的MVVM模式

    在开始介绍angular原理之前,我们有必要先了解下mvvm模式在angular中运用.虽然在angular社区一直将angular统称为前端MVC框架,同时angular团队也称它为MVW(What ...