• 两个点集各取一个点,使距离最小


    题目连接  https://www.acwing.com/problem/content/121/

     

    输入样例:

    2
    4
    0 0
    0 1
    1 0
    1 1
    2 2
    2 3
    3 2
    3 3
    4
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    

    输出样例:

    1.414
    0.000

    https://www.cnblogs.com/-citywall123/p/13174180.html
    参考上篇博客,平面最小点集的变形,把不同集合点标记一下,只有不同集合的两个点才可以求距离,否则返回距离为无穷
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    const double inf = 1e20;
    const int maxn = 100005;
    
    struct Point {
        double x, y;
        int Type;
    }point[maxn];
    
    int n, mpt[maxn];
    
    
    //以x为基准排序
    bool cmpxy(const Point& a, const Point& b) {
        if (a.x != b.x)
            return a.x < b.x;
        return a.y < b.y;
    }
    
    bool cmpy(const int& a, const int& b) {
        return point[a].y < point[b].y;
    }
    
    double min(double a, double b) {
        return a < b ? a : b;
    }
    //求两点距离
    double dis(int i, int j) {
        //只有不同集合的点可以求距离
        if (point[i].Type == point[j].Type)return inf;
    
        return sqrt((point[i].x - point[j].x)*(point[i].x - point[j].x) + (point[i].y - point[j].y)*(point[i].y - point[j].y));
    }
    //将平面二分为左右两个点集,分别找平面左右两边中最短的距离d
    //然后再把平面区间在[left,right]范围内的点中,把p.x到中点mid距离小于d的点标记
    //被标记的点按x坐标再次排序
    //两个for遍历这个点集,更新最小距离d
    double Closest_Pair(int left, int right) {
        double d = inf;
        if (left == right)
            return d;
        if (left + 1 == right)
            return dis(left, right);
        int mid = (left + right) >> 1;
        double d1 = Closest_Pair(left, mid);
        double d2 = Closest_Pair(mid + 1, right);
        d = min(d1, d2);
        int i, j, k = 0;
        //分离出宽度为d的区间
        for (i = left; i <= right; i++) {
            if (fabs(point[mid].x - point[i].x) <= d)
                mpt[k++] = i;
        }
        sort(mpt, mpt + k, cmpy);
        //线性扫描
        for (i = 0; i < k; i++) {
            for (j = i + 1; j < k && point[mpt[j]].y - point[mpt[i]].y < d; j++) {
                double d3 = dis(mpt[i], mpt[j]);
                if (d > d3)    d = d3;
            }
        }
        return d;
    }
    
    int main() {
        int t;
        cin >> t;
        while (t--) {
            int n;
            cin >> n;
            for (int i = 0; i < n; i++) {
                scanf("%lf %lf", &point[i].x, &point[i].y);
                point[i].Type = 0;
            }
            for (int i = n; i < n * 2; i++) {
                scanf("%lf %lf", &point[i].x, &point[i].y);
                point[i].Type = 1;
            }
            n = n * 2;
            sort(point, point + n, cmpxy);
            printf("%.3lf
    ", Closest_Pair(0, n - 1) );
        }
        return 0;
    }
  • 相关阅读:
    DirectX11 With Windows SDK--35 粒子系统
    Xilinx FPGA 的 DNA 加密
    IIC
    蜂鸟E203系列—— SPI 设计
    SPI
    蜂鸟E203系列—— UART 设计
    蜂鸟E203系列——按键中断设计
    蜂鸟E203系列——Windows下运行hello world例程
    蜂鸟E203系列——定时器中断设计
    什么才是定制化IDE的核心价值?
  • 原文地址:https://www.cnblogs.com/-citywall123/p/13174363.html
Copyright © 2020-2023  润新知