1671D.md (2134B)
1 Source: https://codeforces.com/problemset/problem/1671/D 2 3 D. Insert a Progression 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 You are given a sequence of $n$ integers $a_1, a_2, \dots, a_n$. You are also given $x$ integers $1, 2, \dots, x$. 22 23 You are asked to insert each of the extra integers into the sequence $a$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. 24 25 The score of the resulting sequence $a'$ is the sum of absolute differences of adjacent elements in it $\left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right)$. 26 27 What is the smallest possible score of the resulting sequence $a'$? 28 29 Input 30 31 The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases. 32 33 The first line of each testcase contains two integers $n$ and $x$ ($1 \le n, x \le 2 \cdot 10^5$) — the length of the sequence and the number of extra integers. 34 35 The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$). 36 37 The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. 38 39 Output 40 41 For each testcase, print a single integer — the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. 42 43 Example 44 45 Input 46 47 ``` 48 4 49 50 1 5 51 52 10 53 54 3 8 55 56 7 2 10 57 58 10 2 59 60 6 1 5 7 3 3 9 10 10 1 61 62 4 10 63 64 1 3 1 2 65 ``` 66 67 Output 68 69 ``` 70 9 71 15 72 31 73 13 74 ``` 75 76 Note 77 78 Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. 79 80 * $\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10$ 81 * $\underline{7}, 7, \underline{6}, \underline{4}, 2, \underline{2}, \underline{1}, \underline{3}, \underline{5}, \underline{8}, 10$ 82 * $6, \underline{1}, 1, \underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$ 83 * $1, 3, \underline{1}, 1, 2, \underline{2}, \underline{3}, \underline{4}, \underline{5}, \underline{6}, \underline{7}, \underline{8}, \underline{9}, \underline{10}$