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;
    }
  • 相关阅读:
    toj 2975 Encription
    poj 1797 Heavy Transportation
    toj 2971 Rotating Numbers
    zoj 2281 Way to Freedom
    toj 2483 Nasty Hacks
    toj 2972 MOVING DHAKA
    toj 2696 Collecting Beepers
    toj 2970 Hackle Number
    toj 2485 Card Tric
    js页面定位,相关几个属性
  • 原文地址:https://www.cnblogs.com/yohaha/p/6573150.html
Copyright © 2011-2022 走看看