📜  来自给定点的所有对之间距离的平方和

📅  最后修改于: 2021-10-23 08:40:54             🧑  作者: Mango

给定一个由XY 平面N个点的坐标组成的数组arr[] ,任务是找到所有点对之间的距离平方和,即(X i – X j ) 2 + (Y i – Y j ) 2对于每个不同的对(i, j)

例子:

朴素方法:解决问题的最简单方法是生成给定数组arr[][] 的所有可能的不同对,并计算所有点对 (X i , Y j ) 和 (X j ) 之间距离的平方和, Y j ),即 (X i – X j ) 2 + (Y i – Y j ) 2 ,对于每个不同的对(i, j)

时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:为了优化上述方法,其思想是将总和重新分组并将距离的平方和拆分为两个总和。请按照以下步骤解决问题:

  • 初始化变量,比如xqyqxsys
  • 初始化一个变量,比如res ,以存储结果总和。
  • 遍历给定数组,对于每个点{x, y} ,执行以下步骤:
    • 在变量res 中添加(i*x 2 + i*y 2 )的值,这对应于平方距离的添加。
    • 将值(xq – 2 * xs * a)(yq – 2 * ys * b) 添加res以消除(a – b) 2展开式中 2 * X * Y 的影响。
    • 将值a 2b 2分别添加到变量xqyq
    • 将值ab分别添加到变量xsys
    • 将值xsyq分别添加到变量a 2b 2 中
  • 完成上述步骤后,打印res的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the sum of squares
// of distance between all distinct pairs
void findSquareSum(
    int Coordinates[][2], int N)
{
    long long xq = 0, yq = 0;
    long long xs = 0, ys = 0;
 
    // Stores final answer
    long long res = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
        int a, b;
 
        a = Coordinates[i][0];
        b = Coordinates[i][1];
 
        res += xq;
        res -= 2 * xs * a;
 
        // Adding the effect of this
        // point for all the previous
        // x - points
        res += i * (long long)(a * a);
 
        // Temporarily add the
        // square of x-coordinate
        xq += a * a;
        xs += a;
        res += yq;
        res -= 2 * ys * b;
        res += i * (long long)b * b;
 
        // Add the effect of this point
        // for all the previous y - points
        yq += b * b;
        ys += b;
    }
 
    // Print the desired answer
    cout << res;
}
 
// Driver Code
int main()
{
    int arr[][2] = { { 1, 1 },
                     { -1, -1 },
                     { 1, -1 },
                     { -1, 1 } };
    int N = sizeof(arr) / sizeof(arr[0]);
    findSquareSum(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
 
  // Function to find the sum of squares
  // of distance between all distinct pairs
  static void findSquareSum(
    int Coordinates[][], int N)
  {
    long xq = 0, yq = 0;
    long xs = 0, ys = 0;
 
    // Stores final answer
    long res = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
      int a, b;
 
      a = Coordinates[i][0];
      b = Coordinates[i][1];
 
      res += xq;
      res -= 2 * xs * a;
 
      // Adding the effect of this
      // point for all the previous
      // x - points
      res += i * (long)(a * a);
 
      // Temporarily add the
      // square of x-coordinate
      xq += a * a;
      xs += a;
      res += yq;
      res -= 2 * ys * b;
      res += i * (long)b * b;
 
      // Add the effect of this point
      // for all the previous y - points
      yq += b * b;
      ys += b;
    }
 
    // Print the desired answer
    System.out.println(res);
  }
 
 
  // Driver Code
  public static void main(String[] args)
  {
    int arr[][] = { { 1, 1 },
                   { -1, -1 },
                   { 1, -1 },
                   { -1, 1 } };
    int N = arr.length;
    findSquareSum(arr, N);
  }
}
 
// This code is contributed by code_hunt.


Python3
# Python3 program for the above approach
 
# Function to find the sum of squares
# of distance between all distinct pairs
def findSquareSum(Coordinates, N):
    xq , yq = 0, 0
    xs , ys = 0, 0
 
    # Stores final answer
    res = 0
 
    # Traverse the array
    for i in range(N):
 
        a = Coordinates[i][0]
        b = Coordinates[i][1]
 
        res += xq
        res -= 2 * xs * a
 
        # Adding the effect of this
        # point for all the previous
        # x - points
        res += i * (a * a)
 
        # Temporarily add the
        # square of x-coordinate
        xq += a * a
        xs += a
        res += yq
        res -= 2 * ys * b
        res += i * b * b
 
        # Add the effect of this point
        # for all the previous y - points
        yq += b * b
        ys += b
 
    # Print the desired answer
    print (res)
 
# Driver Code
if __name__ == '__main__':
    arr = [ [ 1, 1 ],
         [ -1, -1 ],
         [ 1, -1 ],
         [ -1, 1 ] ]
 
    N = len(arr)
 
    findSquareSum(arr, N)
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the sum of squares
// of distance between all distinct pairs
static void findSquareSum(int[,] Coordinates, int N)
{
    long xq = 0, yq = 0;
    long xs = 0, ys = 0;
     
    // Stores final answer
    long res = 0;
     
    // Traverse the array
    for(int i = 0; i < N ; i++)
    {
        int a, b;
         
        a = Coordinates[i, 0];
        b = Coordinates[i, 1];
         
        res += xq;
        res -= 2 * xs * a;
         
        // Adding the effect of this
        // point for all the previous
         
        // x - points
        res += i * (long)(a * a);
         
        // Temporarily add the
        // square of x-coordinate
        xq += a * a;
        xs += a;
        res += yq;
        res -= 2 * ys * b;
        res += i * (long)b * b;
         
        // Add the effect of this point
        // for all the previous y - points
        yq += b * b;
        ys += b;
    }
     
    // Print the desired answer
    Console.Write(res);
}
 
// Driver code
static void Main()
{
    int[,] arr = { { 1, 1 },
                   { -1, -1 },
                   { 1, -1 },
                   { -1, 1 } };
    int N = arr.GetLength(0);
     
    findSquareSum(arr, N);
}
}
 
// This code is contributed by code_hunt


Javascript


输出:
32

时间复杂度: O(N)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程