HDU 1007 解题报告

题目:

Have you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded.
In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a configuration of the field, you are supposed to find the radius of such a ring.

Assume that all the toys are points on a plane. A point is encircled by the ring if the distance between the point and the center of the ring is strictly less than the radius of the ring. If two toys are placed at the same point, the radius of the ring is considered to be 0.

Input:

The input consists of several test cases. For each case, the first line contains an integer N (2 <= N <= 100,000), the total number of toys in the field. Then N lines follow, each contains a pair of (x, y) which are the coordinates of a toy. The input is terminated by N = 0.

Output:

For each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places.

题目链接: HDU1007

题目分析:

这题就是给定N个点的坐标,寻找最小点对的距离,输出该距离并除二(得到圆环半径)

首先容易想到的是使用两个循环嵌套来遍历整个图以得到最小点对距离,可是这种算法的时间复杂度为O(n^2),绝对会TLE。

然后想到用分治法。如下图,将整个图分成左右两部分,则最小点对只可能出现在左边、右边、或者横跨左右两边。

avatar

假设从左边得到的最短点对距离为MinDistLeft,从右边得到的最短点对距离为MinDistRight,则取两者较小值作为MinDist:

MinDist=min(MinDistLeft,MinDistRight);

然后考虑最小点对横跨左右两边的情况。

若最小点对横跨左右两边,则点对距离大于上面已给出的MinDist的点对我们就可以不予考虑了,因为我们要的是最小点对距离。也就是说,我们要的最小点对只可能出现在x=[mid-MinDist,mid+MinDist]的范围之内,如下图。

avatar

而对于出现在[mid-MinDist,mid+MinDist]的最小点对,其纵坐标值之差也不会超过MinDist(如果超过了就说明这不是最小点对).然后把所有符合特征的点对求出距离与MinDist比较以得出最小点对距离。

下面是我的AC代码,C++模式下通过:


#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#define INF 0xFFFFFFFF

using namespace std;
typedef struct {
    double x;
    double y;
}Node;
double dis(double ax, double ay, double bx, double by)
{
    return sqrt((ax - bx) *(ax - bx) + (ay - by) *(ay - by));
}
double dis(Node a, Node b)
{
    return dis(a.x, a.y, b.x, b.y);
}
double min(double a, double b)
{
    return a < b ? a : b;
}
double findClose(Node *nodes, int start, int end) {
    if (start == end)
        return INF;
    else if (end - start == 1)
        return dis(nodes[start], nodes[end]);

    int mid = (start + end) / 2;

    //分解问题
    double minDistLeft = findClose(nodes, start, mid - 1);
    double minDistRight = findClose(nodes, mid, end);

    double minDist = min(minDistLeft, minDistRight);
    //用于存储跨界情况可能的点下标
    vector<int> *v = new vector<int>;

    for (int i = start; i <= end; i++)
        if (dis(nodes[i].x, 0, nodes[mid].x, 0) <= minDist)
            v->push_back(i);

    sort(v->begin(), v->end(), [nodes, v](int a, int b)->bool { //y轴升序排序
        return nodes[a].y < nodes[b].y;
    });
    for (int i = 0; i < v->size(); i++)
        for (int j = i + 1; j < v->size() && nodes[(*v)[j]].y - nodes[(*v)[i]].y < minDist; j++) //只有当符合条件的才继续循环,一旦发现不合适的点对组合则说明下个点对也不合适
            minDist = min(minDist, dis(nodes[(*v)[j]], nodes[(*v)[i]]));                         
    delete v;
    return minDist;

}
int main()
{
    int points;
    while (cin >> points)
    {
        if (points == 0)
            break;
        Node * node = new Node[points];
        for (int i = 0; i < points; i++)
            cin >> node[i].x >> node[i].y;
        sort(node, node + points, [](Node &a, Node &b)->bool {
            if (a.x != b.x)
                return a.x < b.x;
            else
                return a.y < b.y;
        });
        printf("%.2f\n", findClose(node, 0, points - 1) / 2);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值