CF1453B.Suffix Operations

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Gildong has an interesting machine that has an array aa with nn integers. The machine supports two kinds of operations:

  1. Increase all elements of a suffix of the array by 11.
  2. Decrease all elements of a suffix of the array by 11.

A suffix is a subsegment (contiguous elements) of the array that contains ana_n. In other words, for all ii where aia_i is included in the subsegment, all aja_j's where i<j≤ni \lt j \le n must also be included in the subsegment.

Gildong wants to make all elements of aa equal — he will always do so using the minimum number of operations necessary. To make his life even easier, before Gildong starts using the machine, you have the option of changing one of the integers in the array to any other integer. You are allowed to leave the array unchanged. You want to minimize the number of operations Gildong performs. With your help, what is the minimum number of operations Gildong will perform?

Note that even if you change one of the integers in the array, you should not count that as one of the operations because Gildong did not perform it.

吉隆拥有一台有趣的机器,该机器维护一个包含 nn 个整数的数组 aa。这台机器支持两种操作:

  1. 将数组某个后缀的所有元素增加 11;
  2. 将数组某个后缀的所有元素减少 11。

所谓“后缀”,是指包含 ana_n 的一个子段(即连续的一段元素)。换言之,若某个下标 ii 对应的元素 aia_i 属于该子段,则对所有满足 i<j≤ni < j \le n 的下标 jj,其对应元素 aja_j 也必须属于该子段。

吉隆希望将数组 aa 的所有元素变为相等——他总会以最少的操作次数达成这一目标。为了让他的任务更加轻松,在吉隆开始使用机器之前,你可以选择将数组中的某一个整数修改为任意其他整数(你也可以选择不作任何修改)。你的目标是使吉隆最终执行的操作次数尽可能少。在你的帮助下,吉隆最少需要执行多少次操作?

注意:即使你修改了数组中的某个整数,该修改不计入操作次数,因为这不是吉隆执行的操作。

输入格式

Each test contains one or more test cases. The first line contains the number of test cases tt (1≤t≤10001 \le t \le 1000).

Each test case contains two lines. The first line of each test case consists of an integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5) — the number of elements of the array aa.

The second line of each test case contains nn integers. The ii-th integer is aia_i (−5⋅108≤ai≤5⋅108-5 \cdot 10^8 \le a_i \le 5 \cdot 10^8).

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

每个测试包含一个或多个测试用例。第一行包含测试用例的数量 tt(1≤t≤10001 \le t \le 1000)。

每个测试用例包含两行。每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)—— 表示数组 aa 的元素个数。

每个测试用例的第二行包含 nn 个整数,其中第 ii 个整数为 aia_i(−5⋅108≤ai≤5⋅108-5 \cdot 10^8 \le a_i \le 5 \cdot 10^8)。

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

输出格式

For each test case, print one integer — the minimum number of operations Gildong has to perform in order to make all elements of the array equal.

对于每个测试用例,输出一个整数——Gildong 为使数组所有元素相等所需执行的最少操作次数。

输入输出样例

  • 输入#1

    7
    2
    1 1
    3
    -1 0 2
    4
    99 96 97 95
    4
    -3 -5 -2 1
    6
    1 4 3 2 4 1
    5
    5 0 0 0 5
    9
    -367741579 319422997 -415264583 -125558838 -300860379 420848004 294512916 -383235489 425814447

    输出#1

    0
    1
    3
    4
    6
    5
    2847372102

说明/提示

In the first case, all elements of the array are already equal. Therefore, we do not change any integer and Gildong will perform zero operations.

In the second case, we can set a3a_3 to be 00, so that the array becomes [−1,0,0][-1,0,0]. Now Gildong can use the 22-nd operation once on the suffix starting at a2a_2, which means a2a_2 and a3a_3 are decreased by 11, making all elements of the array −1-1.

In the third case, we can set a1a_1 to 9696, so that the array becomes [96,96,97,95][96,96,97,95]. Now Gildong needs to:

  • Use the 22-nd operation on the suffix starting at a3a_3 once, making the array [96,96,96,94][96,96,96,94].
  • Use the 11-st operation on the suffix starting at a4a_4 22 times, making the array [96,96,96,96][96,96,96,96].

In the fourth case, we can change the array into [−3,−3,−2,1][-3,-3,-2,1]. Now Gildong needs to:

  • Use the 22-nd operation on the suffix starting at a4a_4 33 times, making the array [−3,−3,−2,−2][-3,-3,-2,-2].
  • Use the 22-nd operation on the suffix starting at a3a_3 once, making the array [−3,−3,−3,−3][-3,-3,-3,-3].

在第一种情况下,数组的所有元素已经相等。因此,我们不修改任何整数,Gildong 将执行零次操作。

在第二种情况下,我们可以将 a3a_3 设为 00,使数组变为 [−1,0,0][-1,0,0]。此时 Gildong 可以对从 a2a_2 开始的后缀使用第 22 种操作一次,即 a2a_2 和 a3a_3 各减 11,从而使数组所有元素变为 −1-1。

在第三种情况下,我们可以将 a1a_1 设为 9696,使数组变为 [96,96,97,95][96,96,97,95]。此时 Gildong 需要:

  • 对从 a3a_3 开始的后缀使用一次第 22 种操作,使数组变为 [96,96,96,94][96,96,96,94];
  • 对从 a4a_4 开始的后缀使用两次第 11 种操作,使数组变为 [96,96,96,96][96,96,96,96]。

在第四种情况下,我们可以将数组改为 [−3,−3,−2,1][-3,-3,-2,1]。此时 Gildong 需要:

  • 对从 a4a_4 开始的后缀使用三次第 22 种操作,使数组变为 [−3,−3,−2,−2][-3,-3,-2,-2];
  • 对从 a3a_3 开始的后缀使用一次第 22 种操作,使数组变为 [−3,−3,−3,−3][-3,-3,-3,-3]。

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

首页