📌  相关文章
📜  对具有相同的曼哈顿距离和欧几里德距离

📅  最后修改于: 2021-04-22 02:46:27             🧑  作者: Mango

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

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

注意: 2点对(A,B)与2点对(B,A)相同。

例子:

方法:求解方程式

我们得到x2 = x1或y2 = y1。

考虑3张地图,
1)映射X,其中X [x i ]存储其x坐标等于x i的点数
2)映射Y,其中Y [y i ]存储其y坐标等于y i的点的数量
3)映射XY,其中XY [(X i ,Y i )]存储与点(x i ,y i )重合的点数

现在,
令Xans为所有不同的x i =,具有相同X坐标的对数= X [x i ] 2
对于所有不同的y i,令Yans为具有相同Y坐标的对数= Y [x i ] 2
设XYans为所有不同点(x i ,y i )的重合点数= XY [{x i ,y i }]] 2

因此,所需答案= Xans + Yans – XYans

下面是上述方法的实现:

C++
// C++ implementtaion 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 implementtaion 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)