

Here the equation for the given line is x - y = 0, i.e. a = 1, b = -1 and c = 0.
There are two points, i.e. (-1, 1) and (1, -1).
So, the point on the line that has minimum distances from the points is (0, 0). The sum of distances is sqrt(1 + 1) + sqrt(1 + 1) = sqrt(2) + sqrt(2) = 2.83 (rounding to 2 decimal digits).
The first line contains an integer 'T', which denotes the number of test cases or queries to be run. Then, the T test cases follow.
The first line of each test case contains three space-separated integers denoting a, b and c, respectively, representing the equation of the given line as mentioned above.
The second line of each test case contains a single integer N, denoting the number of coordinates.
Then N lines follow. Each line contains two space-separated integers denoting the x-coordinate and the y-coordinate of the point, respectively.
For each test case, print the minimum possible distance.
Your answer will be considered correct if its absolute or relative error doesn’t exceed 10^-6.
Output for each test case will be printed in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 3000
-100 <= a, b, c, points[i].x, points[i].y <= 100 and (a != 0, b != 0)
Time limit: 1 second
Let’s take a point on the given line which is at an infinite distance from the origin. So, the distances of this point from the given set of points will be large and the sum will be equal to infinite. As we move along the line towards the range of points, this sum starts decreasing gradually.
Then as we move again towards infinity in the opposite direction, the distances start increasing and the sum also reaches infinity. So, this means the sum is minimum at a particular point, and it starts increasing when we move in either direction on the line. If we plot the distance curve’s sum, it looks like a U-shaped curve as stated in the below figure.
So, our idea is to select a search range and search for the optimum points in that range. However, we can’t use linear search as the distances are not necessarily integers. We can’t use binary search as well as the U-shaped curve is not monotonically increasing or decreasing. So, we have to use a ternary search to find the optimum point.
So, we take two values low and high. Initially low contains a very small value and high contains a very large value. These two values denote the range of X-coordinates of the points on the lines. Then we take mid1 and mid2, where mid1 is located at 1/3rd of the search space, and mid2 is located at 2/3rd. Find the Y-coordinate of the point on the line whose X-coordinate is mid1 and mid2 and then find the sum of the distances of the given set of points from this point. We compare both the distances and update the search space accordingly. We keep on doing the above step until the difference between low and high is negligible.
Sorted Doubly Linked List to Balanced BST
Largest Plus Sign
Minimum Operations to Form Letter Y
Matrix Block Sum
Minimized Maximum of Products Distributed to Any Store