CF2153D.Not Alone
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
A circular array b of length m is nice if every element has at least one adjacent element∗ that is equal to it. Formally, for every 1≤i≤m, at least one of the following holds: bi=b(i+m−2)modm+1, or bi=bimodm+1, where xmody denotes the remainder from dividing x by y.
You are given a circular array a of length n. In one operation, you may increase or decrease any element of a by 1. Your task is to determine the minimum number of operations required to make array a nice. More formally, find the minimum value of ∑i=1n∣bi−ai∣ among all nice circular arrays b of length n.
∗In a circular array of length m:
- For each index 2≤i≤m−1, the element at index i is adjacent to the elements at indices i−1 and i+1.
- The element at index 1 is adjacent to the elements at indices 2 and m.
- The element at index m is adjacent to the elements at indices m−1 and 1.
长度为 m 的环形数组 b 被称为“优美的”(nice),当且仅当其中每个元素至少有一个相邻元素∗与其相等。形式化地,对每个 1≤i≤m,以下至少一个条件成立:
bi=b(i+m−2)modm+1或bi=bimodm+1,
其中 xmody 表示 x 除以 y 所得的余数。
你被给定一个长度为 n 的环形数组 a。每次操作中,你可以将 a 的任意一个元素增加或减少 1。你的任务是确定使数组 a 变为优美的所需的最少操作次数。更准确地说,求所有长度为 n 的优美环形数组 b 中,∑i=1n∣bi−ai∣ 的最小值。
∗ 在长度为 m 的环形数组中:
- 对每个下标 2≤i≤m−1,位于下标 i 处的元素与下标 i−1 和 i+1 处的元素相邻;
- 位于下标 1 处的元素与下标 2 和 m 处的元素相邻;
- 位于下标 m 处的元素与下标 m−1 和 1 处的元素相邻。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains a single integer n (3≤n≤2⋅105) — the length of the circular array a.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109) — the elements of the circular array a.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(3≤n≤2⋅105)——表示循环数组 a 的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤109)——表示循环数组 a 的元素。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, output a single integer representing the minimum number of operations required to make array a nice.
对于每个测试用例,输出一个整数,表示使数组 a 变为“好”的所需最少操作次数。
输入输出样例
输入#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 a are equal. Therefore, the circular array a is already nice and no operations are required.
In the second test case, we can perform the following sequence of operations:
- Increase a1 by 1. Now, a=[3,100,99,3].
- Decrease a2 by 1. Now, a=[3,99,99,3].
After these operations, every element has at least one adjacent element with the same value:
- a1 is equal to a4.
- a2 is equal to a3.
- a3 is equal to a2.
- a4 is equal to a1.
In the third test case, the circular array a can become nice by decreasing a4 four times. This results in a=[2,2,5,5,5] which is nice as every element has at least one adjacent element with the same value.
在第一个测试用例中,数组 a 的所有元素均相等。因此,循环数组 a 已经是“优美的”,无需执行任何操作。
在第二个测试用例中,我们可以执行以下操作序列:
- 将 a1 增加 1。此时,a=[3,100,99,3]。
- 将 a2 减少 1。此时,a=[3,99,99,3]。
执行这些操作后,每个元素至少有一个相邻元素与其值相等:
- a1 等于 a4。
- a2 等于 a3。
- a3 等于 a2。
- a4 等于 a1。
在第三个测试用例中,通过将 a4 减少四次,循环数组 a 可变为“优美的”。结果为 a=[2,2,5,5,5],该数组是“优美的”,因为每个元素至少有一个相邻元素与其值相等。
输入解题思路,AI测评打分。不知道怎么写?