#K62197. Minimum Teleportation Cost
Minimum Teleportation Cost
Minimum Teleportation Cost
You are given several test cases. In each test case, you are given n points on a number line. Your task is to determine the minimum cost required to bring all points to the same coordinate using teleportation moves.
For each test case, you will be provided an integer n denoting the number of points, followed by n space separated integers representing the coordinates of the points. The cost to move a point from coordinate x to coordinate y is \(|x-y|\). You can choose any coordinate as the meeting point. It can be proved that choosing the median (or the left median in case of an even number of points) minimizes the total cost. Formally, if the sorted points are \(a_1, a_2, \dots, a_n\), then the optimal meeting coordinate is given by: \[ M = \begin{cases} a_{\frac{n+1}{2}}, & \text{if } n \text{ is odd},\\ a_{\frac{n}{2}}, & \text{if } n \text{ is even (using the left median)}. \end{cases} \] The total minimum cost is \(\sum_{i=1}^{n} |a_i - M|\).
inputFormat
The input is given via standard input (stdin) and has the following format:
T n1 p11 p12 ... p1n1 n2 p21 p22 ... p2n2 ... nT pT1 pT2 ... pTnT
Where:
T
is the number of test cases.- For each test case, the first line contains an integer
n
(the number of points), followed by a line containingn
space-separated integers representing the coordinates.
outputFormat
For each test case, output a single integer representing the minimum cost required to bring all points to the same coordinate. The answers for different test cases should be printed on separate lines using standard output (stdout).
## sample2
3
1 3 5
4
-1 0 1 2
4
4
</p>