HDOJ-1007 Quoit Design(最接近点问题)

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <cstdio>

using namespace std;

#define maxn 100005
#define INF 1e20
struct Point{
    double x, y;
}p[maxn];
bool cmp(const Point &a, const Point &b){
    return a.x < b.x;
}
bool temp(int i, int j){
    return p[i].y < p[j].y;
}
double Cal(int i, int j){
    return sqrt((p[i].x - p[j].x) * (p[i].x - p[j].x) + 
            (p[i].y - p[j].y) * (p[i].y - p[j].y));
}
double Closed(int l, int r){

    if(l == r)
      return INF;
    if(l == r-1)
      return Cal(l, r);

    int mid = (l + r) >> 1;
    double d = min(Closed(l, mid), Closed(mid+1, r));
    vector<int> v;
    for(int i = l; i <= r; i++)
      if(fabs(p[i].x - p[mid].x) < d)
         v.push_back(i);

    sort(v.begin(), v.end(), temp);
    for(int i = 0; i < v.size(); i++)
       for(int j = i+1; j < v.size() && fabs(p[v[i]].y-p[v[j]].y) < d; j++)
       d = min(d, Cal(v[i], v[j]));

    return d;
}
int main(){

// freopen("in.txt", "r", stdin);
    int n;

    while(cin >> n && n){

        for(int i = 0; i < n; i++){
           scanf("%lf%lf", &p[i].x, &p[i].y);
        }
        sort(p, p+n, cmp);
        printf("%.2f\n", Closed(0, n-1)/2.0);
    }

    return 0;
} 

你可能感兴趣的:(HDOJ-1007 Quoit Design(最接近点问题))