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\n", sqrt(ans)/2);
    }
    return 0;
}

 

posted on 2017-03-18 12:48  yohaha  阅读(62)  评论(0编辑  收藏

导航

统计