#K54642. Minimum Operations to Uniform Decoration
Minimum Operations to Uniform Decoration
Minimum Operations to Uniform Decoration
You are given T test cases. For each test case, you are given an integer N and an array of N integers representing the decoration values in a series of villages. In one operation, you can adjust a village's decoration value by 1. Your task is to compute the minimum number of operations required so that all villages have the same decoration value.
To achieve this, you should choose a target value equal to the median of the array. The total number of operations needed is given by the formula:
\(\displaystyle \text{operations} = \sum_{i=1}^{N} |x_i - m|\)
where \(m\) is the median of the decoration values \(x_1, x_2, \dots, x_N\).
Read the input from stdin
and output your answer to stdout
.
inputFormat
The first line contains a single integer T denoting the number of test cases. Each test case consists of two lines:
- The first line contains an integer N (the number of villages).
- The second line contains N integers representing the decoration values.
All input is read from stdin
.
outputFormat
For each test case, output a single line containing the minimum number of operations required so that all decoration values are equal. The answer for each test case should be printed on a new line to stdout
.
1
1
5
0
</p>