CF2229A.Slimes on a Line
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There are n slimes on a line, where slime i is at position ai on the line. You will perform the following operation some number of times (possibly none):
- select an integer x, then for each j (1≤j≤n):
- if aj<x then do aj:=aj+1.
- if aj>x then do aj:=aj−1.
- if aj=x then do nothing.
Determine the minimum number of operations to make all slimes occupy the same position.
数轴上有 n 只史莱姆,其中第 i 只史莱姆位于位置 ai。你可以执行以下操作若干次(可以为零次):
- 选择一个整数 x,然后对每个 j(1≤j≤n):
- 若 aj<x,则令 aj:=aj+1;
- 若 aj>x,则令 aj:=aj−1;
- 若 aj=x,则不进行任何操作。
求使所有史莱姆占据同一位置所需的最少操作次数。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤100). The description of the test cases follows.
The first line of each testcase contains an integer n (2≤n≤1000) — the number of slimes.
The second line of each testcase contains n integers a1,a2,…,an (1≤ai≤1000) — the initial positions of the slimes.
It is guaranteed that the sum of n over all test cases does not exceed 1000.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤100)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(2≤n≤1000)—— 表示史莱姆的数量。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤1000)—— 表示史莱姆的初始位置。
保证所有测试用例的 n 之和不超过 1000。
输出格式
For each testcase, output the minimum number of operations required to make all slimes occupy the same position.
对于每个测试用例,输出使所有史莱姆占据同一位置所需的最少操作次数。
输入输出样例
输入#1
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
输出#1
2 0 3 2 4 4 4 5 5 500
说明/提示
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].
Test Case 2: All the slimes are already at position 3, and hence 0 operations are needed.
测试用例 1:我们可以执行 2 次操作,且每次操作均取 x=3。第一次操作将位置数组更新为 a=[2,3,3,3,4],第二次操作再将其更新为 a=[3,3,3,3,3]。
测试用例 2:所有史莱姆已位于位置 3,因此无需执行任何操作。
输入解题思路,AI测评打分。不知道怎么写?