📜  具有相同曼哈顿距离和欧几里得距离的配对

📅  最后修改于: 2021-10-27 08:44:04             🧑  作者: Mango

在给定的笛卡尔平面中,有 N 个点。任务是找到点对数(A,B),使得

  • A点和B点不重合。
  • 点之间的曼哈顿距离和欧几里得距离应该相等。

注意:一对 2 点(A,B)被认为与一对 2 点(B,A)相同。

例子:

方法:关于求解方程

我们得到, x2 = x1 或 y2 = y1。
考虑3张地图,
1) Map X,其中 X[x i ] 存储 x 坐标等于 x i 的点数
2) 映射 Y,其中 Y[y i ] 存储其 y 坐标等于 y i 的点数
3) Map XY,其中XY[(X i , Y i )] 存储与点(x i , y i )重合的点数
现在,
令 Xans 为具有相同 X 坐标的对数 = X[x i ] 2对于所有不同的 x i =
令 Yans 为所有不同 y i具有相同 Y 坐标的对数 = Y[x i ] 2
令 XYans 为所有不同点 (x i , y i ) 的重合点数 = XY[{x i , y i }] 2
因此所需的答案 = Xans + Yans – XYans
下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to return the number of non coincident
// pairs of points with manhattan distance
// equal to euclidean distance
int findManhattanEuclidPair(pair arr[], int n)
{
    // To store frequency of all distinct Xi
    map X;
 
    // To store Frequency of all distinct Yi
    map Y;
 
    // To store Frequency of all distinct
    // points (Xi, Yi);
    map, int> XY;
 
    for (int i = 0; i < n; i++) {
        int xi = arr[i].first;
        int yi = arr[i].second;
 
        // Hash xi coordinate
        X[xi]++;
 
        // Hash yi coordinate
        Y[yi]++;
 
        // Hash the point (xi, yi)
        XY[arr[i]]++;
    }
 
    int xAns = 0, yAns = 0, xyAns = 0;
 
    // find pairs with same Xi
    for (auto xCoordinatePair : X) {
        int xFrequency = xCoordinatePair.second;
 
        // calculate ((xFrequency) C2)
        int sameXPairs =
             (xFrequency * (xFrequency - 1)) / 2;
        xAns += sameXPairs;
    }
 
    // find pairs with same Yi
    for (auto yCoordinatePair : Y) {
        int yFrequency = yCoordinatePair.second;
 
        // calculate ((yFrequency) C2)
        int sameYPairs =
                (yFrequency * (yFrequency - 1)) / 2;
        yAns += sameYPairs;
    }
 
    // find pairs with same (Xi, Yi)
    for (auto XYPair : XY) {
        int xyFrequency = XYPair.second;
  
        // calculate ((xyFrequency) C2)
        int samePointPairs =
             (xyFrequency * (xyFrequency - 1)) / 2;
        xyAns += samePointPairs;
    }
 
    return (xAns + yAns - xyAns);
}
 
// Driver Code
int main()
{
    pair arr[] = {
        { 1, 2 },
        { 2, 3 },
        { 1, 3 }
    };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << findManhattanEuclidPair(arr, n) << endl;
    return 0;
}


Python3
# Python3 implementation of the
# above approach
from collections import defaultdict
 
# Function to return the number of
# non coincident pairs of points with
# manhattan distance equal to
# euclidean distance
def findManhattanEuclidPair(arr, n):
 
    # To store frequency of all distinct Xi
    X = defaultdict(lambda:0)
 
    # To store Frequency of all distinct Yi
    Y = defaultdict(lambda:0)
 
    # To store Frequency of all distinct
    # points (Xi, Yi)
    XY = defaultdict(lambda:0)
 
    for i in range(0, n):
        xi = arr[i][0]
        yi = arr[i][1]
 
        # Hash xi coordinate
        X[xi] += 1
 
        # Hash yi coordinate
        Y[yi] += 1
 
        # Hash the point (xi, yi)
        XY[tuple(arr[i])] += 1
     
    xAns, yAns, xyAns = 0, 0, 0
 
    # find pairs with same Xi
    for xCoordinatePair in X:
        xFrequency = X[xCoordinatePair]
 
        # calculate ((xFrequency) C2)
        sameXPairs = (xFrequency *
                     (xFrequency - 1)) // 2
        xAns += sameXPairs
     
    # find pairs with same Yi
    for yCoordinatePair in Y:
        yFrequency = Y[yCoordinatePair]
 
        # calculate ((yFrequency) C2)
        sameYPairs = (yFrequency *
                     (yFrequency - 1)) // 2
        yAns += sameYPairs
 
    # find pairs with same (Xi, Yi)
    for XYPair in XY:
        xyFrequency = XY[XYPair]
     
        # calculate ((xyFrequency) C2)
        samePointPairs = (xyFrequency *
                         (xyFrequency - 1)) // 2
        xyAns += samePointPairs
     
    return (xAns + yAns - xyAns)
 
# Driver Code
if __name__ == "__main__":
 
    arr = [[1, 2], [2, 3], [1, 3]]
     
    n = len(arr)
 
    print(findManhattanEuclidPair(arr, n))
     
# This code is contributed by Rituraj Jain


输出:
2

时间复杂度:O(NlogN),其中 N 是点数
空间复杂度:O(N)