zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    poj3635(最短路)
    poj 3041( 最大匹配)
    poj 3522(生成树)
    poj 1904(强连通分量)
    poj 2446(二分匹配)
    poj 2400(最小权匹配)
    poj 2175(费用流消圈)
    poj 1256(搜索)
    poj 2195(最小费用流)
    poj 3613(最短路)
  • 原文地址:https://www.cnblogs.com/hh13579/p/13759478.html
Copyright © 2011-2022 走看看