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;
    }
  • 相关阅读:
    GCD
    SQLite
    将博客搬至CSDN
    Extjs 4 总结
    spring mvc 复杂参数注入
    7/12 聊天室结束
    7/10
    7/6一些知识点
    随便写写
    spring boot 入门操作(三)
  • 原文地址:https://www.cnblogs.com/yohaha/p/6573150.html
Copyright © 2011-2022 走看看