• P1429 平面最近点对(加强版)


    二维平面最近点

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<cmath>
    #include<string>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N = 2e5 + 10;
    const int INF = 2e9;
    int n,temp[N];
    struct Point{
        double x,y;
    }S[N];
    bool cmp(const Point &a,const Point &b)
    {
        if(a.x == b.x)
        {
            return a.y < b.y;
        }
        return a.x < b.x;
    }
    bool cmps(const int &a,const int &b)
    {
        return S[a].y < S[b].y;
    }
    double dist(int i,int j)
    {
        double x = pow((S[i].x - S[j].x),2);
        double y = pow((S[i].y - S[j].y),2);
    //    cout << x << ' ' << y << '
    ';
        return sqrt(x+y);
    }
    double merge(int left,int right)
    {
        double d = INF;
        if(left == right)
            return d;
        if(left + 1 == right)
        {
            double dd = dist(left,right);
    //        cout << left << " " << right << " " << dd << "
    ";
            return dd;
        }
        int mid = (left + right) / 2;
        double d1 = merge(left,mid);
        double d2 = merge(mid+1,right);
        d = min(d1,d2);
        int k = 0;
        for(int i=left;i<=right;i++)
        {
            if(fabs(S[mid].x - S[i].x) < d)
            {
                temp[k++] = i;
            }
        }
        sort(temp,temp+k,cmps);
        for(int i=0;i<k;i++)
        {
            for(int j=i+1;fabs(S[temp[j]].y - S[temp[i]].y) < d && j < k;j++)
            {
                d = min(d,dist(temp[i],temp[j]));
            }
        }
        return d;
    }
    int main()
    {
        cin >> n;
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&S[i].x,&S[i].y);
        }
        sort(S,S+n,cmp);
        printf("%0.4lf
    ",merge(0,n-1));
        return 0;
    }
    
  • 相关阅读:
    Remove Element
    String StringBuffer StringBuilder
    Length of Last Word
    Space Replacement
    Longest Palindromic Substring
    jQuery2.0.0版本以后不再支持ie8的原因
    npm命令
    不借助第三个变量实现两个变量交换及原理分析
    js 不同进制之间相互转换
    ECMAScript toString() 方法
  • 原文地址:https://www.cnblogs.com/hh13579/p/13759478.html
Copyright © 2020-2023  润新知