hdu(1007) 最近点对 分治法

    最近点对一般想到枚举  ,一一枚举时间复杂度为n^2;枚举时候一些操作是多余的,有了分治算法的思想 ,把一些问题分个击破,再回到整体。


题目链接


  以这道题为例,我们可以把他按照x轴的升序分成多个子区域先在子区域中求最近点距离,然后将相邻两个子区域合并,看看两个子区域中有没有更小的。大致思想就是这样的。

设计算法:递归将问题分成一小个问题。在找区域里面的最近点先将他按照x或y轴升或者降序排序这样找就可以省时间,详见代码,先看题,再看代码。


有错的地方还望指出,多多来hack

#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
const int MM = 100005;
const double INF=1e20;
struct Points
{
    double x,y;
};
Points point[MM];
bool cmpx(Points x,Points y)
{
    return x.x>1;
    d=min(closest_point(l,mid),closest_point(mid+1,r));//递归分解问题,找到子区域中的最近点距离
    /*****************找到后,以他们的最近距离来分割区间************************/
    int i,j,cut=0;
    for(i=l; i<=r; i++)
    {
        if(fabs(point[i].x-point[mid].x)=d)
                break;
            //sort之后,只要当前超过了了,后面的数字一点不可能比这个小,所以不找,省时间
            d=min(dist(point[index[i]],point[index[j]]),d);
        }
    }
    return d;
}
int main()
{
    while(scanf("%d",&n)!=EOF&&n)
    {
        int i;
        for(i=0; i


你可能感兴趣的:(算法,题解)