CF2229A.Slimes on a Line

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are nn slimes on a line, where slime ii is at position aia_i on the line. You will perform the following operation some number of times (possibly none):

  • select an integer xx, then for each jj (1≤j≤n1 \le j \le n):
    • if aj<xa_j \lt x then do aj:=aj+1a_j := a_j + 1.
    • if aj>xa_j \gt x then do aj:=aj−1a_j := a_j - 1.
    • if aj=xa_j = x then do nothing.

Determine the minimum number of operations to make all slimes occupy the same position.

数轴上有 nn 只史莱姆,其中第 ii 只史莱姆位于位置 aia_i。你可以执行以下操作若干次(可以为零次):

  • 选择一个整数 xx,然后对每个 jj(1≤j≤n1 \le j \le n):
    • 若 aj<xa_j \lt x,则令 aj:=aj+1a_j := a_j + 1;
    • 若 aj>xa_j \gt x,则令 aj:=aj−1a_j := a_j - 1;
    • 若 aj=xa_j = x,则不进行任何操作。

求使所有史莱姆占据同一位置所需的最少操作次数。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1001 \le t \le 100). The description of the test cases follows.

The first line of each testcase contains an integer nn (2≤n≤10002 \le n \le 1000) — the number of slimes.

The second line of each testcase contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_{n} (1≤ai≤10001 \le a_i \le 1000) — the initial positions of the slimes.

It is guaranteed that the sum of nn over all test cases does not exceed 10001000.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1001 \le t \le 100)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤10002 \le n \le 1000)—— 表示史莱姆的数量。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_{n}(1≤ai≤10001 \le a_i \le 1000)—— 表示史莱姆的初始位置。

保证所有测试用例的 nn 之和不超过 10001000。

输出格式

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 22 operations, both with x=3x = 3. The first operation updates the array of positions to a=[2,3,3,3,4]a = [2, 3, 3, 3, 4], and then the second operation updates it to a=[3,3,3,3,3]a = [3, 3, 3, 3, 3].

Test Case 2: All the slimes are already at position 33, and hence 00 operations are needed.

测试用例 1:我们可以执行 22 次操作,且每次操作均取 x=3x = 3。第一次操作将位置数组更新为 a=[2,3,3,3,4]a = [2, 3, 3, 3, 4],第二次操作再将其更新为 a=[3,3,3,3,3]a = [3, 3, 3, 3, 3]。

测试用例 2:所有史莱姆已位于位置 33,因此无需执行任何操作。

输入解题思路,AI测评打分。不知道怎么写?

首页