1746C.md (2269B)
1 Source: https://codeforces.com/problemset/problem/1746/C 2 3 C. Permutation Operations 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 permutation $a$ of size $n$ and you should perform $n$ operations on it. In the $i$-th operation, you can choose a non-empty suffix of $a$ and increase all of its elements by $i$. How can we perform the operations to minimize the number of inversions in the final array? 22 23 Note that you can perform operations on the same suffix any number of times you want. 24 25 A permutation of size $n$ is an array of size $n$ such that each integer from $1$ to $n$ occurs exactly once in this array. A suffix is several consecutive elements of an array that include the last element of the array. An inversion in an array $a$ is a pair of indices $(i, j)$ such that $i \gt j$ and $a_{i} \lt a_{j}$. 26 27 Input 28 29 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. 30 31 The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the size of the array. 32 33 The second line contains $n$ distinct integers $a_{1}, a_{2}, \dots, a_{n}$ ($1 \le a_i \le n$), the initial permutation $a$. 34 35 It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. 36 37 Output 38 39 For each test case, print $n$ integers $x_{1}, x_{2}, \ldots, x_{n}$ ($1 \le x_{i} \le n$ for each $1 \le i \le n$) indicating that the $i$-th operation must be applied to the suffix starting at index $x_{i}$. If there are multiple answers, print any of them. 40 41 Example 42 43 Input 44 45 ``` 46 4 47 48 4 49 50 1 2 3 4 51 52 5 53 54 1 3 2 4 5 55 56 3 57 58 2 3 1 59 60 1 61 62 1 63 ``` 64 65 Output 66 67 ``` 68 1 1 1 1 69 1 4 3 2 1 70 1 3 3 71 1 72 ``` 73 74 Note 75 76 In the first test case one of the optimal solutions is to increase the whole array on each operation (that is, choose the suffix starting at index $1$). The final array $[11, 12, 13, 14]$ contains $0$ inversions. 77 78 In the second test case, $a$ will be equal to $[2, 4, 3, 5, 6]$, $[2, 4, 3, 7, 8]$, $[2, 4, 6, 10, 11]$, $[2, 8, 10, 14, 15]$ and $[7, 13, 15, 19, 20]$ after the first, second, third, fourth, and fifth operations, respectively. So the final array $a$ has zero inversions.