CF2153D.Not Alone

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

A circular array bb of length mm is nice if every element has at least one adjacent element∗^{\text{∗}} that is equal to it. Formally, for every 1≤i≤m1\le i\le m, at least one of the following holds: bi=b(i+m−2) mod m  +1b_i = b_{(i+m-2)\bmod m\;+1}, or bi=bi mod m  +1b_i = b_{i\bmod m\;+1}, where x mod yx \bmod y denotes the remainder from dividing xx by yy.

You are given a circular array aa of length nn. In one operation, you may increase or decrease any element of aa by 11. Your task is to determine the minimum number of operations required to make array aa nice. More formally, find the minimum value of ∑i=1n∣bi−ai∣\sum_{i=1}^n |b_i - a_i| among all nice circular arrays bb of length nn.

∗^{\text{∗}}In a circular array of length mm:

  • For each index 2≤i≤m−12\le i\le m - 1, the element at index ii is adjacent to the elements at indices i−1i - 1 and i+1i + 1.
  • The element at index 11 is adjacent to the elements at indices 22 and mm.
  • The element at index mm is adjacent to the elements at indices m−1m - 1 and 11.

长度为 mm 的环形数组 bb 被称为“优美的”(nice),当且仅当其中每个元素至少有一个相邻元素∗^{\text{∗}}与其相等。形式化地,对每个 1≤i≤m1\le i\le m,以下至少一个条件成立:

bi=b(i+m−2) mod m  +1或bi=bi mod m  +1,b_i = b_{(i+m-2)\bmod m\;+1} \quad \text{或} \quad b_i = b_{i\bmod m\;+1},

其中 x mod yx \bmod y 表示 xx 除以 yy 所得的余数。

你被给定一个长度为 nn 的环形数组 aa。每次操作中,你可以将 aa 的任意一个元素增加或减少 11。你的任务是确定使数组 aa 变为优美的所需的最少操作次数。更准确地说,求所有长度为 nn 的优美环形数组 bb 中,∑i=1n∣bi−ai∣\sum_{i=1}^n |b_i - a_i| 的最小值。

∗^{\text{∗}} 在长度为 mm 的环形数组中:

  • 对每个下标 2≤i≤m−12\le i\le m - 1,位于下标 ii 处的元素与下标 i−1i - 1 和 i+1i + 1 处的元素相邻;
  • 位于下标 11 处的元素与下标 22 和 mm 处的元素相邻;
  • 位于下标 mm 处的元素与下标 m−1m - 1 和 11 处的元素相邻。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (3≤n≤2⋅1053\le n\le 2\cdot 10^5) — the length of the circular array aa.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1091\le a_i\le 10^9) — the elements of the circular array aa.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

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

每个测试用例的第一行包含一个整数 nn(3≤n≤2⋅1053\le n\le 2\cdot 10^5)——表示循环数组 aa 的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1091\le a_i\le 10^9)——表示循环数组 aa 的元素。

保证所有测试用例的 nn 之和不超过 2⋅1052\cdot 10^5。

输出格式

For each test case, output a single integer representing the minimum number of operations required to make array aa nice.

对于每个测试用例,输出一个整数,表示使数组 aa 变为“好”的所需最少操作次数。

输入输出样例

  • 输入#1

    4
    5
    1 1 1 1 1
    4
    2 100 99 3
    5
    2 2 5 9 5
    6
    1 1 1 2 1 2

    输出#1

    0
    2
    4
    1

说明/提示

In the first test case, all elements of aa are equal. Therefore, the circular array aa is already nice and no operations are required.

In the second test case, we can perform the following sequence of operations:

  • Increase a1a_1 by 11. Now, a=[3,100,99,3]a=[3, 100, 99, 3].
  • Decrease a2a_2 by 11. Now, a=[3,99,99,3]a=[3, 99, 99, 3].

After these operations, every element has at least one adjacent element with the same value:

  • a1a_1 is equal to a4a_4.
  • a2a_2 is equal to a3a_3.
  • a3a_3 is equal to a2a_2.
  • a4a_4 is equal to a1a_1.

In the third test case, the circular array aa can become nice by decreasing a4a_4 four times. This results in a=[2,2,5,5,5]a = [2, 2, 5, 5, 5] which is nice as every element has at least one adjacent element with the same value.

在第一个测试用例中,数组 aa 的所有元素均相等。因此,循环数组 aa 已经是“优美的”,无需执行任何操作。

在第二个测试用例中,我们可以执行以下操作序列:

  • 将 a1a_1 增加 11。此时,a=[3,100,99,3]a=[3, 100, 99, 3]。
  • 将 a2a_2 减少 11。此时,a=[3,99,99,3]a=[3, 99, 99, 3]。

执行这些操作后,每个元素至少有一个相邻元素与其值相等:

  • a1a_1 等于 a4a_4。
  • a2a_2 等于 a3a_3。
  • a3a_3 等于 a2a_2。
  • a4a_4 等于 a1a_1。

在第三个测试用例中,通过将 a4a_4 减少四次,循环数组 aa 可变为“优美的”。结果为 a=[2,2,5,5,5]a = [2, 2, 5, 5, 5],该数组是“优美的”,因为每个元素至少有一个相邻元素与其值相等。

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

首页