algorithm - 分而治之算法来计算 friend 点数

标签 algorithm

我有一个问题需要分而治之解决。 有一个包含 N 个点的集合 S。 如果有一个平行于轴的正方形,只包含S中的两个点p1和p2,则我们称p1和p2为 friend 点。

现在,我需要使用分而治之算法来计算 S 中有多少 friend 点。

我想了很久。我没有办法。 我需要你的帮助。 我的英语不好,如果你有问题,请问我,我会补充。谢谢。

最佳答案

伪代码中的以下(不一定是最优的)算法怎么样?

List<Pair<Point, Point>> findFriends(List<Point> points)
{
    List<Pair<Point, Point>> friends = new List<Pair<Point, Point>>();

    if (points.Count > 1)
    {
        if (points.Count == 2)
        {
           friends.Add(new Pair<Point, Point>(points[0], points[1]);
        }
        else
        {
           int xMedian = getMedianX(points);
           int yMedian = getMedianY(points);
           List<Point> topLeftPoints = selectPoints(points, 0, xMedian-1, yMedian, yMax);
           List<Point> bottomLeftPoints = selectPoints(points, 0, xMedian-1, 0, yMedian-1);
           List<Point> topRightPoints = selectPoints(points, xMedian, xMax, yMedian, yMax);
           List<Point> bottomRightPoints = selectPoints(points, xMedian, xMax, yMedian, yMax);

           friends.Add(findFriends(topLeftPoints));
           friends.Add(findFriends(bottomLeftPoints));
           friends.Add(findFriends(topRightPoints));
           friends.Add(findFriends(bottomRightPoints));      
        }
    }

    return friends;
}

关于algorithm - 分而治之算法来计算 friend 点数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42921949/

相关文章:

algorithm - 这种类型的软件可以吗

algorithm - 覆盖 n 个点的最小半径为 r 的圆圈数

javascript - 一个关于时间复杂度计算和实时消耗的谜题

c# - 找到搜索数据结构字符串比较的最佳方法

algorithm - 在不到 O(n^2) 的时间内模拟许多物体之间的重力

python - 图遍历,也许是另一种数学?

algorithm - 解决这个问题的更好方法是什么?

c - 使用 C 中的回溯算法求解数独

c++ - 优化或新算法来解决这个问题?

algorithm - 如果记忆化是自上而下的深度优先,而 DP 是自下而上的广度优先,那么自上而下的广度优先/自下而上的深度优先等价物是什么?