为什么第二个样例已知wa,我用的上交的最近点对板子

P1429 平面最近点对(加强版)

wj123 @ 2019-10-11 21:46:41

#include<bits/stdc++.h>

using namespace std;

const int maxn = 1e6+5;
const double eps = 1e-8;
int cmp(double x){
    if(fabs(x) < eps) return 0;
    if(x > 0) return 1;
    return -1;
}
inline double sqr(double x){
    return x*x;
}
struct Point{
    double x, y;
    Point() {}
    Point(double a, double b) : x(a), y(b) {}
    void input(){
        scanf("%lf%lf", &x, &y);
    }
    friend Point operator + (const Point &a, const Point &b){
        return Point(a.x + b.x, a.y + b.y);
    }
    friend Point operator - (const Point &a, const Point &b){
        return Point(a.x - b.x, a.y - b.y);
    }
    friend bool operator == (const Point &a, const Point &b){
        return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0;
    }
    friend Point operator * (const Point &a, const double &b){
        return Point(a.x * b, a.y * b);
    }
    friend Point operator * (const double &a, const Point &b){
        return Point(a * b.x, a * b.y);
    }
    friend Point operator / (const Point &a, const double &b){
        return Point(a.x / b, a.y / b);
    }
    double norm(){
        return sqrt(sqr(x) + sqr(y));
    }
};
Point a[maxn];
int n, s[maxn];

bool cmpx(int i, int j){
    return cmp(a[i].x - a[j].x) < 0;
}

bool cmpy(int i, int j){
    return cmp(a[i].y - a[j].y) < 0;
}

double min_dist(Point a[], int s[], int l, int r){
    double ans = 1e100;
    if(r - l < 20){
        for(int q = l; q < r; q++)
            for(int w = q+1; w < r; w++)
                ans = min(ans, (a[s[q]] - a[s[w]]).norm());
        return ans;
    }
    int tl, tr, m = (l + r) / 2;
    ans = min(min_dist(a, s, l, m), min_dist(a, s, m, r));
    for(tl = l; a[s[tl]].x < a[s[m]].x - ans; tl++);
    for(tr = r - 1; a[s[tr]].x > a[s[m]].x + ans; tr--);
    sort(s+tl, s+tr, cmpy);
    for(int q = tl; q < tr; q++)
        for(int w = q+1 ; w < min(tr, q+6); w++)
            ans = min(ans, (a[s[q]] - a[s[w]]).norm());
    sort(s+tl, s+tr, cmpx);

    return ans;
}

double Min_Dist(Point a[], int s[], int n){
    for(int i = 0; i < n; i++) s[i] = i;
    sort(s, s+n, cmpx);
    return min_dist(a, s, 0, n);
}

int main()
{
//  while(scanf("%d", &n) != EOF && n){
        scanf("%d", &n);
        for(int i = 0; i < n; i++){
            double x, y;
            scanf("%lf%lf", &x, &y);
            a[i] = Point(x, y);
        //  s[i] = i;
        }
        //sort(s, s+n, cmpx);
        printf("%.4lf\n", Min_Dist(a, s, n));
//  }

    return 0;
}

|