线上的史莱姆AI (CI翻译)
There are $n$ slimes on a line, where slime $i$ is at position $a_i$ on the line. You will perform the following operation some number of times (possibly none):
有 $n$ 个史莱姆位于一条直线上,其中史莱姆 $i$ 位于直线上的位置 $a_i$。你可以执行以下操作任意次(也可以一次都不执行):AI (CI翻译)
- select an integer
$x$, then for each$j$($1 \le j \le n$): - if
$a_j \lt x$then do$a_j := a_j + 1$.- if
$a_j \gt x$then do$a_j := a_j - 1$. - if
$a_j = x$then do nothing.
- if
- 选择一个整数
$x$,然后对于每个$j$($1 \le j \le n$): - 如果
$a_j \lt x$,则执行$a_j := a_j + 1$。- 如果
$a_j \gt x$,则执行$a_j := a_j - 1$。 - 如果
$a_j = x$,则不执行任何操作。
- 如果
Determine the minimum number of operations to make all slimes occupy the same position.
求使所有史莱姆位于同一位置所需的最少操作次数。AI (CI翻译)
Input
输入格式
AI (CI翻译)Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows.
每个测试包含多个测试用例。第一行包含测试用例的数量 $t$($1 \le t \le 100$)。接下来描述各个测试用例。AI (CI翻译)
The first line of each testcase contains an integer $n$ ($2 \le n \le 1000$) — the number of slimes.
每个测试用例的第一行包含一个整数 $n$($2 \le n \le 1000$),表示史莱姆的数量。AI (CI翻译)
The second line of each testcase contains $n$ integers $a_1,a_2,\ldots,a_{n}$ ($1 \le a_i \le 1000$) — the initial positions of the slimes.
每个测试用例的第二行包含 $n$ 个整数 $a_1,a_2,\ldots,a_{n}$($1 \le a_i \le 1000$),表示史莱姆的初始位置。AI (CI翻译)
It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$.
保证所有测试用例中 $n$ 的总和不超过 $1000$。AI (CI翻译)
Output
输出格式
AI (CI翻译)For each testcase, output the minimum number of operations required to make all slimes occupy the same position.
对于每个测试用例,输出使所有史莱姆位于同一位置所需的最少操作次数。AI (CI翻译)
Note
说明
AI (CI翻译)Test Case 1: We can perform $2$ operations, both with $x = 3$. The first operation updates the array of positions to $a = [2, 3, 3, 3, 4]$, and then the second operation updates it to $a = [3, 3, 3, 3, 3]$.
测试用例 1: 我们可以执行 $2$ 次操作,两次都选择 $x = 3$。第一次操作后,位置数组变为 $a = [2, 3, 3, 3, 4]$;第二次操作后,位置数组变为 $a = [3, 3, 3, 3, 3]$。AI (CI翻译)
Test Case 2: All the slimes are already at position $3$, and hence $0$ operations are needed.
测试用例 2: 所有史莱姆已经位于位置 $3$,因此不需要执行任何操作。AI (CI翻译)
样例
10
5
1 2 3 4 5
5
3 3 3 3 3
6
5 6 7 1 2 3
2
2 5
4
1 3 8 7
4
6 2 1 8
3
1 3 9
5
1 10 1 10 10
8
10 8 5 9 1 6 9 10
2
1 1000
2
0
3
2
4
4
4
5
5
500
题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。