1 条题解

  • 1
    @ 2021-8-7 18:41:45

    C++ :

    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<algorithm>
    #include<cmath>
    #define INF 0x3f3f3f3f
    using namespace std;
    typedef long long int ll;
    const int N = 2e5 + 10;
    struct p{
        double x, y;
        int type;
    }a[N], b[N];
    bool cmp(p a, p b)
    {
        if(a.x == b.x)
            return a.y < b.y;
        return a.x < b.x;
    }
    bool cmp1(p a, p b)
    {
        if(a.y == b.y)
            return a.x < b.x;
        return a.y < b.y;
    }
    double dis(p a, p b)
    {
        if(a.type == b.type)return INF;
        double x = a.x - b.x, y = a.y - b.y;
        return sqrt(x * x + y * y);
    }
    double solve(int l, int r)
    {
        if(l == r)return INF;
        if(r - l == 1)
            return dis(a[l], a[r]);
        int mid = (l + r) >> 1;
        double ans = min(solve(l, mid), solve(mid + 1, r));
        int k = 0;
        for(int i = l; i <= r; i++)
            if(a[i].x >= a[mid].x - ans && a[i].x <= a[mid].x + ans)
                b[k++] = a[i];
        sort(b, b + k, cmp1);
        for(int i = 0; i < k; i++){
            for(int j = i + 1; j < k; j++){
                if(b[j].y >= b[i].y + ans)
                    break;
                ans = min(ans, dis(a[i], a[j]));
            }
        }
        return ans;
    }
    int main()
    {
        int t, n;
        cin >>t;
        while(t--){
            scanf("%d", &n);
            for(int i = 0; i < n; i++){
                scanf("%lf%lf", &a[i].x, &a[i].y);
                a[i].type = 0;
            }
            for(int i = n; i < 2 * n; i++){
                scanf("%lf%lf", &a[i].x, &a[i].y);
                a[i].type = 1;
            }
            n *= 2;
            sort(a, a + n, cmp);
            printf("%0.3f\n", solve(0, n - 1));
        }
        return 0;
    }
    
    • 1

    信息

    ID
    30
    时间
    1000ms
    内存
    128MiB
    难度
    1
    标签
    递交数
    83
    已通过
    66
    上传者