1729C.md (2853B)
1 Source: https://codeforces.com/problemset/problem/1729/C 2 3 C. Jumping on Tiles 4 5 time limit per test 6 7 1 second 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 Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $s$. 22 23 In other words, you are given a string $s$ consisting of lowercase Latin letters. 24 25 Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $i$-th tile to $j$-th tile has a cost equal to $|index(s_i) - index(s_j)|$, where $index(c)$ is the index of the letter $c$ in the alphabet (for example, $index($'a'$)=1$, $index($'b'$)=2$, ..., $index($'z'$)=26$) . 26 27 Polycarp wants to get to the $n$-th tile for the minimum total cost, but at the same time make maximum number of jumps. 28 29 In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps. 30 31 Polycarp can visit each tile at most once. 32 33 Polycarp asks you to help — print the sequence of indices of string $s$ on which he should jump. 34 35 Input 36 37 The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test. 38 39 Each test case is given by the string $s$ ($2 \le |s| \le 2 \cdot 10^5$), where $|s|$ — is the length of string $s$. The string $s$ consists of lowercase Latin letters. 40 41 It is guaranteed that the sum of string lengths $s$ over all test cases does not exceed $2 \cdot 10^5$. 42 43 Output 44 45 The answer to each test case consists of two lines. 46 47 In the first line print two integers $cost$, $m$, where $cost$ is the minimum total cost of the path, and $m$ is the maximum number of visited tiles Polycarp can make to get to $n$-th tiles for the minimum total cost $cost$ (i.e. the number of jumps is $m-1$). 48 49 In the next line print $m$ different numbers $j_1, j_2, \dots, j_m$ ($1 \le j_i \le |s|$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $1$ (that is, $j_1=1$) and the last number must be the value of $|s|$ (that is, $j_m=|s|$). 50 51 If there are multiple answers, print any of them. 52 53 Example 54 55 Input 56 57 ``` 58 6 59 60 logic 61 62 codeforces 63 64 bca 65 66 aaaaaaaaaaa 67 68 adbaadabad 69 70 to 71 ``` 72 73 Output 74 75 ``` 76 9 4 77 1 4 3 5 78 16 10 79 1 8 3 4 9 5 2 6 7 10 80 1 2 81 1 3 82 0 11 83 1 8 10 4 3 5 7 2 9 6 11 84 3 10 85 1 9 5 4 7 3 8 6 2 10 86 5 2 87 1 2 88 ``` 89 90 Note 91 92 In the first test case, the required path corresponds to the picture: 93 94  95 96 In this case, the minimum possible total cost of the path is achieved. Since $index($'l'$)=12$, $index($'o'$)=15$, $index($'g'$)=7$, $index($'i'$)=9$, $index($'c'$)=3$, then the total cost of the path is $|12-9|+|9-7|+|7-3|=3+2+4=9$.