zoukankan      html  css  js  c++  java
  • hdu 1007 Quoit Design 分治求最近点对

    #include <bits/stdc++.h>
    using namespace std;
    
    pair <double, double> a[100005], tmp[100005];
    double ans;
    double getDis(const pair<double, double>& lhs, const pair<double, double>& rhs)
    {
        return (lhs.first-rhs.first)*(lhs.first-rhs.first) + (lhs.second-rhs.second)*(lhs.second-rhs.second);
    }
    void merge(int l, int r)
    {
        if (l >= r)
            return ;
        if (l == r - 1)
        {
            ans = min(ans, getDis(a[l], a[r]));
            return ;
        }
        int mid = l + r >> 1;
        merge(l, mid);
        merge(mid, r);
        int cnt = 0;
        for (int i = l; i <= r; i++)
        {
            if (a[i].first >= a[mid].first-ans && a[i].first <= a[mid].first+ans)
                tmp[cnt++] = a[i];
        }
        sort(tmp, tmp+cnt, [](const pair<double, double>& lhs, const pair<double, double>& rhs)
                                {
                                    return lhs.second < rhs.second;
                                });
        for (int i = 0; i < cnt; i++)
        {
            for (int j = i + 1; j < cnt; j++)
            {
                if (tmp[j].second - tmp[i].second >= ans)
                    break;
                ans = min(ans, getDis(tmp[i], tmp[j]));
            }
        }
    }
    int main()
    {
        int n;
        while (cin >> n && n)
        {
            for (int i = 0; i < n; i++)
            {
                scanf("%lf%lf", &a[i].first, &a[i].second);
    
            }
            sort(a, a+n);
            ans = 9e18;
            merge(0, n-1);
            printf("%.2f
    ", sqrt(ans)/2);
        }
        return 0;
    }
  • 相关阅读:
    POJ 2255. Tree Recovery
    Ural 1011. Conductors
    Ural 1010. Discrete Function
    算法导论学习 之 解递归式
    算法导论学习 之 渐进符号
    kubernetes-集群构建
    kubernetes-集群备份和恢复
    kubernetes-概念
    Kubernetes-常用命令
    kubernetes-单机实验(入门)
  • 原文地址:https://www.cnblogs.com/yohaha/p/6573150.html
Copyright © 2011-2022 走看看