1453B.md (3204B)
1 Source: https://codeforces.com/problemset/problem/1453/B 2 3 B. Suffix Operations 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 512 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Gildong has an interesting machine that has an array $a$ with $n$ integers. The machine supports two kinds of operations: 22 23 1. Increase all elements of a suffix of the array by $1$. 24 2. Decrease all elements of a suffix of the array by $1$. 25 26 A suffix is a subsegment (contiguous elements) of the array that contains $a_n$. In other words, for all $i$ where $a_i$ is included in the subsegment, all $a_j$'s where $i \lt j \le n$ must also be included in the subsegment. 27 28 Gildong wants to make all elements of $a$ 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? 29 30 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. 31 32 Input 33 34 Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). 35 36 Each test case contains two lines. The first line of each test case consists of an integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of elements of the array $a$. 37 38 The second line of each test case contains $n$ integers. The $i$-th integer is $a_i$ ($-5 \cdot 10^8 \le a_i \le 5 \cdot 10^8$). 39 40 It is guaranteed that the sum of $n$ in all test cases does not exceed $2 \cdot 10^5$. 41 42 Output 43 44 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. 45 46 Example 47 48 Input 49 50 ``` 51 7 52 2 53 1 1 54 3 55 -1 0 2 56 4 57 99 96 97 95 58 4 59 -3 -5 -2 1 60 6 61 1 4 3 2 4 1 62 5 63 5 0 0 0 5 64 9 65 -367741579 319422997 -415264583 -125558838 -300860379 420848004 294512916 -383235489 425814447 66 ``` 67 68 Output 69 70 ``` 71 0 72 1 73 3 74 4 75 6 76 5 77 2847372102 78 ``` 79 80 Note 81 82 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. 83 84 In the second case, we can set $a_3$ to be $0$, so that the array becomes $[-1,0,0]$. Now Gildong can use the $2$-nd operation once on the suffix starting at $a_2$, which means $a_2$ and $a_3$ are decreased by $1$, making all elements of the array $-1$. 85 86 In the third case, we can set $a_1$ to $96$, so that the array becomes $[96,96,97,95]$. Now Gildong needs to: 87 88 * Use the $2$-nd operation on the suffix starting at $a_3$ once, making the array $[96,96,96,94]$. 89 * Use the $1$-st operation on the suffix starting at $a_4$ $2$ times, making the array $[96,96,96,96]$. 90 91 In the fourth case, we can change the array into $[-3,-3,-2,1]$. Now Gildong needs to: 92 93 * Use the $2$-nd operation on the suffix starting at $a_4$ $3$ times, making the array $[-3,-3,-2,-2]$. 94 * Use the $2$-nd operation on the suffix starting at $a_3$ once, making the array $[-3,-3,-3,-3]$.