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

    给一系列点的坐标,求距离最小的两个点。

    基本是照着别人的代码写的……我以为我看懂了的……怎么还是错了辣么多次……

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cmath>
    
    using namespace std;
    
    const int MAX = 100005;
    
    struct point
    {
        double x, y;
    }p[MAX];
    
    int pt[MAX];
    
    bool cmpx(point a, point b)         //点按横坐标排序
    {
        return a.x < b.x;           
    }
    
    bool cmpy(int a, int b)             //按纵坐标排序
    {
        return p[a].y < p[b].y;         //卧槽我以前是这么写的return p[pt[a]].y < p[pt[b]].y;竟然曾经能ac
    }
    
    double dis(point a, point b)        //求两点距离
    {
        return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
    }
    
    double min_dis(int l, int r)        //求a[l]和a[r]之间的最短距离
    {
        if (r - l == 1)
        {
            return dis(p[l], p[r]);
        }
        if (r - l == 2)                 //三个点时求三段距离的最小值
        {
            return min(min(dis(p[l], p[l + 1]), dis(p[l + 1], p[r])), dis(p[l], p[r]));
        }
        int mid = (l + r) >> 1;
        double ans = min(min_dis(l, mid), min_dis(mid + 1, r));    //把点按横坐标分为两部分,求左边的最小值和右边的最小值
        int cnt = 0;
        for (int i = l; i <= r; i++)    //两部分之间可能有最小值,只有横坐标之差小于ans时才有可能
        {
            if (p[i].x - p[mid].x <= ans && p[mid].x - p[i].x <= ans)
            {
                pt[cnt++] = i;
            }
        }
        sort(pt, pt + cnt, cmpy);
        for (int i = 0; i < cnt; i++)
        {
            for (int j = i + 1; j < cnt; j++)
            {
                if (p[pt[j]].y - p[pt[i]].y > ans)          //已经按纵坐标排序,这里加判断能节省时间
                    break;
                ans = min(ans, dis(p[pt[j]], p[pt[i]]));
            }
        }
        return ans;
    }
    
    int main()
    {
        int n;
        while (scanf("%d", &n) == 1 && n)
        {
            for (int i = 0; i < n; i++)
            {
                scanf("%lf%lf", &p[i].x, &p[i].y);
            }
            sort(p, p + n, cmpx);
            printf("%.2f
    ", min_dis(0, n - 1) / 2);
        }
        return 0;
    }
    

      

    细节啊细节!!循环里的i写成l一直错555~错了n次真的是……

  • 相关阅读:
    remove white space from read
    optimize the access speed of django website
    dowload image from requests
    run jupyter from command
    crawl wechat page
    python version 2.7 required which was not found in the registry windows 7
    health
    alternate rows shading using conditional formatting
    word
    【JAVA基础】static 关键字
  • 原文地址:https://www.cnblogs.com/wenruo/p/4492698.html
Copyright © 2011-2022 走看看