zoukankan      html  css  js  c++  java
  • HDU 1007 Quoit Design

    题目链接:HDU 1007 Quoit Design

    题目大意:
    求一堆点里最小的两点间距,然后再除以(2)

    题解:
    由于数据比较水,对横坐标排序后求相邻几个点的距离就过了。
    正确做法参考了网上的题解:参考题解,将代码改成了自己的码风。

    #include <iostream>
    #include <cmath>
    #include <algorithm>
    #include <iomanip>
    using namespace std;
    #define io_speed_up ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MAXN 100005
    
    struct Point {
        double x, y;
    } px[MAXN], py[MAXN];
    
    double getDis(Point p1, Point p2) {
        return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
    }
    
    bool cmpx(Point p1, Point p2) {
        return p1.x < p2.x;
    }
    
    bool cmpy(Point p1, Point p2) {
        return p1.y < p2.y;
    }
    
    double closest(int s, int e) {
        if (s + 1 == e) {
            return getDis(px[s], px[e]);
        }
        if (s + 2 == e) {
            return min(getDis(px[s], px[s + 1]), min(getDis(px[s + 1], px[e]), getDis(px[s], px[e])));
        }
        int mid = s + e >> 1;
        double ans = min(closest(s, mid), closest(mid + 1, e)); // 分治
        int cnt = 0;
        for (int i = s; i <= e; ++i) { // 把x坐标在px[mid].x-ans~px[mid].x+ans范围内的点取出来
            if (px[i].x >= px[mid].x - ans && px[i].x <= px[mid].x + ans) {
                py[cnt++] = px[i];
            }
        }
        sort(py, py + cnt, cmpy); // 按y坐标排序
        for (int i = 0; i < cnt; ++i) {
            for (int j = i + 1; j < cnt; ++j) { // py数组中的点是按照y坐标升序的
                if (py[j].y - py[i].y >= ans) {
                    break;
                }
                ans = min(ans, getDis(py[i], py[j]));
            }
        }
        return ans;
    }
    
    int n;
    
    int main() {
        io_speed_up; // 或者改成scanf
        while (cin >> n && n) {
            for (int i = 0; i < n; ++i) {
                cin >> px[i].x >> px[i].y;
            }
            sort(px, px + n, cmpx);
            double dis = closest(0, n - 1) / 2;
            cout << fixed << setprecision(2);
            cout << dis << endl;
        }
        return 0;
    }
    
  • 相关阅读:
    python无意中发现的
    mysql 中关于周和月份的表示
    列表表达式
    python编码
    bugfree安装
    python练习题代码
    根据用户名或者厂商名称生成相关的弱口令
    获取QQ企业邮箱通讯录PY脚本
    SQL注入POC
    乌云精华漏洞爬取匹配
  • 原文地址:https://www.cnblogs.com/IzumiSagiri/p/13833324.html
Copyright © 2011-2022 走看看