给定一个大小为N * 3的矩阵arr[][] ,每行包含 3 个定义元素的属性,即(x-coordinate, y-coordinate, speed)和两个整数X和Y ,任务是计数点(X, Y)处可能的碰撞对的数量,如果给定数组中的每个元素都以各自的速度沿直线向给定点(X, Y) 移动。
例子:
Input: arr[] = [ [5, 12, 1], [16, 63, 5], [-10, 24, 2], [7, 24, 2], [-24, 7, 2] ], X = 0, Y = 0
Output: 4
Explanation:
Possible collisions are between the following pair of elements: (arr[0], arr[1]), (arr[0], arr[2]), (arr[1], arr[2]) and (arr[3], arr[4])
Hence, total collisions are 4.
Input: arr[] = [ [1, 42, 9] ], X = 1, Y = 1
Output: 0
Explanation:
Since a single point is present, there will be no collisions.
Hence, total collisions are 0.
方法:我们的想法是简单地找到一条其每一点将达到给定的点(X,Y)的时间。如果两点同时到达原点,那么它们肯定会发生碰撞。现在,要计算每个点所花费的时间,请执行以下操作:
For a given point (x, y) and speed S
Distance D is given by:
D = = √ ((x2 – x1)2 + (y2 – y1)2) = √ ((x)2 + (y)2)
Time = Distance/Speed, squaring on both sides to eliminate root from distance,
Time2 = Distance2/Speed2
在为每个点计算时间2后,只需检查它们中的哪些相同并计算碰撞次数。
以下是步骤:
- 遍历数组arr[] 。
- 创建一个新数组Time[]并为每个点计算时间并附加到该数组并对其进行排序。
- Traverse Time[]并计算同时到达给定点的元素数量,使用该计数计算当时的碰撞次数。
- 最后,返回碰撞总数。
下面是上述方法的实现:
C++14
// C++14 program to implement
// the above approach
#include
using namespace std;
// Function to find the count of
// possible pairs of collisions
int solve(vector> &D, int N,
int X, int Y)
{
// Stores the time at which
// points reach the origin
vector T;
// Calculate time for each point
for(int i = 0; i < N; i++)
{
int x = D[i][0];
int y = D[i][1];
double speed = D[i][2];
double time = ((x * x - X * X) +
(y * y - Y * Y)) /
(speed * speed);
T.push_back(time);
}
// Sort the times
sort(T.begin(), T.end());
int i = 0;
int total = 0;
// Counting total collisions
while (i < T.size() - 1)
{
// Count of elements arriving at
// a given point at the same time
int count = 1;
while (i < T.size() - 1 and
T[i] == T[i + 1])
{
count += 1;
i += 1;
}
total += (count * (count - 1)) / 2;
i += 1;
}
return total;
}
// Driver Code
int main()
{
int N = 5;
// Given set of points with speed
vector> D = { { 5, 12, 1 },
{ 16, 63, 5 },
{ -10, 24, 2 },
{ 7, 24, 2 },
{ -24, 7, 2 } };
int X = 0, Y = 0;
// Function call
cout << (solve(D, N, X, Y));
return 0;
}
// This code is contributed by mohit kumar 29
Java
// Java program to implement
// the above approach
import java.util.*;
import java.lang.*;
class GFG{
// Function to find the count of
// possible pairs of collisions
static double solve(int[][] D, int N,
int X, int Y)
{
// Stores the time at which
// points reach the origin
ArrayList T = new ArrayList<>();
// Calculate time for each point
for(int i = 0; i < N; i++)
{
int x = D[i][0];
int y = D[i][1];
double speed = D[i][2];
double time = ((x * x - X * X) +
(y * y - Y * Y)) /
(speed * speed);
T.add(time);
}
// Sort the times
Collections.sort(T);
int i = 0;
int total = 0;
// Counting total collisions
while (i < (T.size() - 1))
{
// Count of elements arriving at
// a given point at the same time
int count = 1;
while ((i < (T.size() - 1)) &&
(Double.compare(T.get(i),
T.get(i + 1)) == 0))
{
count += 1;
i += 1;
}
total += (count * (count - 1)) / 2;
i += 1;
}
return total;
}
// Driver Code
public static void main (String[] args)
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = 5;
// Given set of points with speed
int[][] D = { { 5, 12, 1 },
{ 16, 63, 5 },
{ -10, 24, 2 },
{ 7, 24, 2 },
{ -24, 7, 2 } };
int X = 0, Y = 0;
// Function call
System.out.println(solve(D, N, X, Y));
}
}
// This code is contributed by offbeat
Python3
# Python3 Program to implement
# the above approach
# Function to find the count of
# possible pairs of collisions
def solve(D, N, X, Y):
# Stores the time at which
# points reach the origin
T = []
# Calculate time for each point
for i in range(N):
x = D[i][0]
y = D[i][1]
speed = D[i][2]
time = ((x * x - X * X) +
(y * y - Y * Y)) / (speed * speed)
T.append(time)
# Sort the times
T.sort()
i = 0
total = 0
# Counting total collisions
while i
C#
// C# program to implement
// the above approach
using System;
using System.Collections;
class GFG{
// Function to find the count of
// possible pairs of collisions
static double solve(int[, ] D, int N,
int X, int Y)
{
// Stores the time at which
// points reach the origin
ArrayList T = new ArrayList();
// Calculate time for each point
for(int i = 0; i < N; i++)
{
int x = D[i, 0];
int y = D[i, 1];
double speed = D[i, 2];
double time = ((x * x - X * X) +
(y * y - Y * Y)) /
(speed * speed);
T.Add(time);
}
// Sort the times
T.Sort();
int j = 0;
int total = 0;
// Counting total collisions
while (j < (T.Count - 1))
{
// Count of elements arriving at
// a given point at the same time
int count = 1;
while ((j < (T.Count - 1)) &&
(Convert.ToDouble(T[j]).CompareTo(
Convert.ToDouble(T[j + 1])) == 0))
{
count += 1;
j += 1;
}
total += (count * (count - 1)) / 2;
j += 1;
}
return total;
}
// Driver Code
public static void Main (String[] args)
{
int N = 5;
// Given set of points with speed
int [,] D = new int [,] { { 5, 12, 1 },
{ 16, 63, 5 },
{ -10, 24, 2 },
{ 7, 24, 2 },
{ -24, 7, 2 } };
int X = 0, Y = 0;
// Function call
Console.WriteLine(solve(D, N, X, Y));
}
}
// This code is contributed by jana_sayantan
Javascript
4.0
时间复杂度: O(N log N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。