Mock vita 2020 problem

Can anybody help me with the solution ?

COLLISION COURSE
Problem Description
On a busy road, multiple cars are passing by. A simulation is run to see what happens if brakes fail for all cars on the road. The only way for them to be safe is if they don’t collide and pass by each other. The goal is to identify whether any of the given cars would collide or pass by each other safely around a Roundabout. Think of this as a reference point O ( Origin with coordinates (0,0) ), but instead of going around it, cars pass through it.

Considering that each car is moving in a straight line towards the origin with individual uniform speed. Cars will continue to travel in that same straight line even after crossing origin. Calculate the number of collisions that will happen in such a scenario.

Note : - Calculate collisions only at origin. Ignore the other collisions. Assume that each car continues on its respective path even after the collision without change of direction or speed for an infinite distance.

Constraints
1<=C<=10^5

-10^9 <= x,y <= 10^9

0 < v < 10^9.

Input Format
The first line contains an integer C, denoting the number of cars being considered that are passing by around the origin.

Next C lines contain 3 space delimited values, first two of them being for position coordinates (x,y) in 2D space and the third one for speed (v).

Output
A single integer Q denoting the number of collisions at origin possible for given set of cars.

Timeout
1

Test Case
Example 1

Input

5

5 12 1

16 63 5

-10 24 2

7 24 2

-24 7 2

Output

4

Explanation

Let the 5 cars be A, B, C, D, and E respectively.

4 Collisions are as follows -

  1. A & B.

  2. A & C.

  3. B & C.

  4. D & E.

Total distance for each car will be sqrt(x^2 + y^2 ). Now speed of each car is given. So, you can calculate time to reach origin. Now see which pairs have same time and count them.
Like for 1, t=13/1
for 2, t=65/5
for 3, t=26/2…

1 Like

yeah i did exactly what you said but didn’t get accepted.Any other approach?

I think WA was because of precision error. You can do two things.
First if you are calc in fractional form then add 0.0001 and take its int . But I am not sure that this will always give AC.
Second, store the fractional value in pair form (num,denm). But devide both of them by gcd then store and compare.(num=x^2+y^2 and denm=v^2)

Since x and y squared will be 10^18, instead of doing sqrt(x^2+y^2)/v
try this sqrt((x/v)^2+(y/v)^2)

1 Like