zoukankan      html  css  js  c++  java
  • HDU1007(求近期两个点之间的距离)

    一年前学长讲这题的时候,没听懂。自己搜解题报告也看不懂,放了一年。

    现在对分治和递归把握的比一年前更加熟悉,这题也就攻克了。

    题意:给你一堆点让你求近期两点之间距离的一半,假设用暴力的话O(n*n)明显会超时。那么我们就用分治思想,将全部点依照横坐标x排序,然后取中间的mid。分着求1-mid,mid-n,这样递归求解,递归仅仅须要logn级别就能够完毕递归。这有点类似二分思想。

    1.我们取左边点对最小和右边点对最小比較,取最小的,记做d

    2:可是近期点有可能一个点在左边。一个点在右边,所以要单独考虑。

    3:我们for循环遍历left->right的每一个点,取出当中横坐标满足到中间mid这个点横坐标之差小于d的点。由于求距离还有将y算上。假设你x都大于d了,就不可能距离小于d

    4:可是假设满足这种点非常多的话,还是会超时,那么我们还须要做优化,我们将这些满足3的点再依照y坐标排序,然后再求距离,假设有y坐标差大于d的话,就跳出,由于后面的话都是大于y。不存在更小的。所以尽管是两层for循环,事实上复杂度并不高

    5:总的来说时间复杂度是O(nlogn)


    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <iostream>
    #include <math.h>
    using namespace std;
    const double eps = 1e-6;
    const int MAXN = 100010;
    const double INF = 1e20;
    struct Point
    {
        double x,y;
    };
    double dist(Point a,Point b)
    {
        return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
    }
    Point p[MAXN];
    Point tmpt[MAXN];
    bool cmpxy(Point a,Point b)
    {
        if(a.x != b.x)return a.x < b.x;
        else return a.y < b.y;
    }
    bool cmpy(Point a,Point b)
    {
        return a.y < b.y;
    }
    double Closest_Pair(int left,int right)
    {
        double d = INF;
        if(left == right)return d;
        if(left + 1 == right)
            return dist(p[left],p[right]);
        int mid = (left+right)/2;
        double d1 = Closest_Pair(left,mid);
        double d2 = Closest_Pair(mid+1,right);
        d = min(d1,d2);
        int k = 0;
        for(int i = left; i <= right; i++)
        {
            if(fabs(p[mid].x - p[i].x) <= d)
                tmpt[k++] = p[i];
        }
        sort(tmpt,tmpt+k,cmpy);
        for(int i = 0; i <k; i++)
        {
            for(int j = i+1; j < k && tmpt[j].y - tmpt[i].y < d; j++)
            {
                d = min(d,dist(tmpt[i],tmpt[j]));
            }
        }
        return d;
    }
    
    int main()
    {
        int n;
        while(scanf("%d",&n)==1 && n)
        {
            for(int i = 0; i < n; i++)
                scanf("%lf%lf",&p[i].x,&p[i].y);
            sort(p,p+n,cmpxy);
            printf("%.2lf
    ",Closest_Pair(0,n-1)/2);
        }
        return 0;
    }
    


  • 相关阅读:
    Java.io 包(字节流)
    Java 集合框架(常用数据结构)
    Java.util 包(Date 类、Calendar类、Random类)
    Java.lang 包 (包装类、String类、Math类、Class类、Object类)
    Java 多态(接口)
    maxcompute troubleshoot
    maxcompute
    文件命名
    weblogic修改ServerName
    设计模式---策略模式
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/6816953.html
Copyright © 2011-2022 走看看