2200D.md (3649B)
1 Source: https://codeforces.com/problemset/problem/2200/D 2 3 D. Portal 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$^{\text{∗}}$ $p$ of length $n$. There are also two portals located at positions $x$ and $y$ ($x \lt y$). 22 23 A portal at position $i$ is initially located between the $i$-th and $(i+1)$-th elements of the array. Specifically, if $i=0$, then the portal is located before the first element of the array, and if $i=n$, then the portal is located after the last element. 24 25 You may perform either of the following two operations as many times as you like: 26 27 1. Remove the element to the immediate left of one portal and insert it to the immediate right of the other portal. 28 2. Remove the element to the immediate right of one portal and insert it to the immediate left of the other portal. 29 30 Let $\mathbf{\color{red}{\mathcal{O}}}$ denote a portal. For example, if $p$ is $[3,\mathbf{\color{red}{\mathcal{O}}},2,4,\mathbf{\color{red}{\mathcal{O}}},1]$: 31 32 * Using operation $1$ on the left and right portals respectively results in the arrays $[\mathbf{\color{red}{\mathcal{O}}},2,4,\mathbf{\color{red}{\mathcal{O}}},3,1]$ and $[3,\mathbf{\color{red}{\mathcal{O}}},4,2,\mathbf{\color{red}{\mathcal{O}}},1]$. 33 * Using operation $2$ on the left and right portals respectively results in the arrays $[3,\mathbf{\color{red}{\mathcal{O}}},4,2,\mathbf{\color{red}{\mathcal{O}}},1]$ and $[3,1,\mathbf{\color{red}{\mathcal{O}}},2,4,\mathbf{\color{red}{\mathcal{O}}}]$. 34 35 Find the lexicographically$^{\text{†}}$ smallest permutation you can obtain using these operations. Note that portals do not affect the lexicographical comparison of permutations. 36 37 $^{\text{∗}}$A permutation of length $n$ is an array of length $n$ containing each integer from $1$ to $n$ exactly once. 38 39 $^{\text{†}}$A permutation $a$ is lexicographically smaller than permutation $b$ if there exists an index $i$ such that $a_j = b_j$ for all indices $1 \leq j \lt i$ and $a_i \lt b_i$. 40 41 Input 42 43 The first line contains an integer $t$ ($1 \leq t \leq 2\cdot 10^4$) — the number of test cases. 44 45 For each test case, the first line contains three integers $n$, $x$, and $y$ ($1 \leq n \leq 2 \cdot 10^5$, $0 \leq x \lt y \leq n$). 46 47 The second line of each test case contains $n$ integers $p_1, p_2, \dots, p_n$ — a permutation of length $n$. 48 49 The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. 50 51 Output 52 53 For each test case, output a line with $n$ integers — the lexicographically smallest permutation you can obtain. 54 55 Example 56 57 Input 58 59 ``` 60 4 61 62 4 0 4 63 64 3 1 4 2 65 66 3 1 2 67 68 3 2 1 69 70 5 1 3 71 72 1 3 5 2 4 73 74 2 0 1 75 76 1 2 77 ``` 78 79 Output 80 81 ``` 82 1 4 2 3 83 84 2 3 1 85 86 1 2 3 5 4 87 88 1 2 89 ``` 90 91 Note 92 93 Let $\mathbf{\color{red}{\mathcal{O}}}$ denote a portal. 94 95 In the first test case, the array is $[\mathbf{\color{red}{\mathcal{O}}},3,1,4,2,\mathbf{\color{red}{\mathcal{O}}}]$. Using operation $2$ on the left portal results in $[\mathbf{\color{red}{\mathcal{O}}},1,4,2,3,\mathbf{\color{red}{\mathcal{O}}}]$, which is the lexicographically smallest possible permutation that can be obtained. 96 97  The operation described above. 98 99 In the second test case, the array is $[3,\mathbf{\color{red}{\mathcal{O}}},2,\mathbf{\color{red}{\mathcal{O}}},1]$. Using operation $1$ on the left portal results in $[\mathbf{\color{red}{\mathcal{O}}},2,\mathbf{\color{red}{\mathcal{O}}},3,1]$, which is the lexicographically smallest possible permutation that can be obtained. 100 101 In the fourth test case, it is optimal not to do any operations.