2128B.md (3150B)
1 Source: https://codeforces.com/problemset/problem/2128/B 2 3 B. Deque Process 4 5 time limit per test 6 7 1.5 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 We say that an array $a$ of size $n$ is bad if and only if there exists $1 \leq i \leq n - 4$ such that one of the following conditions holds: 22 23 * $a_i \lt a_{i+1} \lt a_{i+2} \lt a_{i+3} \lt a_{i+4}$ 24 * $a_i \gt a_{i+1} \gt a_{i+2} \gt a_{i+3} \gt a_{i+4}$ 25 26 An array is good if and only if it's not bad. For example: 27 28 * $a = [3, \color{red}{1, 2, 4, 5, 6}]$ is bad because $a_2 \lt a_3 \lt a_4 \lt a_5 \lt a_6$. 29 * $a = [\color{red}{7, 6, 5, 4, 1}, 2, 3]$ is bad because $a_1 \gt a_2 \gt a_3 \gt a_4 \gt a_5$. 30 * $a = [7, 6, 5, 1, 2, 3, 4]$ is good. 31 32 You're given a permutation$^{\text{∗}}$ $p_1, p_2, \ldots, p_n$. 33 34 You must perform $n$ turns. At each turn, you must remove either the leftmost or the rightmost remaining element in $p$. Let $q_i$ be the element removed at the $i$-th turn. 35 36 Choose which element to remove at each turn so that the resulting array $q$ is good. We can show that under the given constraints, it's always possible. 37 38 $^{\text{∗}}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array). 39 40 Input 41 42 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10\,000$). The description of the test cases follows. 43 44 The first line of each test case contains a single integer $n$ ($5 \leq n \leq 100\,000$) — the length of the array. 45 46 The second line of each test case contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq n$, $p_i$ are pairwise distinct) — elements of the permutation. 47 48 It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200\,000$. 49 50 Output 51 52 For each test case, you must output a string $s$ of length $n$. For every $1 \leq i \leq n$, at the $i$-th turn: 53 54 * $s_i = \texttt{L}$ means that you removed the leftmost element of $p$ 55 * $s_i = \texttt{R}$ means that you removed the rightmost element of $p$ 56 57 We can show that an answer always exists. If there are multiple solutions, print any of them. 58 59 Example 60 61 Input 62 63 ``` 64 6 65 66 7 67 68 1 2 3 4 5 6 7 69 70 9 71 72 1 3 6 8 9 7 5 4 2 73 74 12 75 76 1 2 11 3 6 4 7 8 12 5 10 9 77 78 6 79 80 4 1 2 5 6 3 81 82 5 83 84 1 2 3 5 4 85 86 9 87 88 5 1 8 6 2 7 9 4 3 89 ``` 90 91 Output 92 93 ``` 94 RRRLLLL 95 LLRRLLRRL 96 LLLLLLLLLLLL 97 LLLLLL 98 LLLLL 99 LLLLLLLLL 100 ``` 101 102 Note 103 104 In the first test case, the sequence $\color{blue}{\texttt{RRR}}\color{red}{\texttt{LLLL}}$ results in $q = [\color{blue}{7}, \color{blue}{6}, \color{blue}{5}, \color{red}{1}, \color{red}{2}, \color{red}{3}, \color{red}{4}]$. 105 106 In the second test case, the sequence $\color{red}{\texttt{LL}}\color{blue}{\texttt{RR}}\color{red}{\texttt{LL}}\color{blue}{\texttt{RR}}\color{red}{\texttt{L}}$ results in $q = [\color{red}{1}, \color{red}{3}, \color{blue}{2}, \color{blue}{4}, \color{red}{6}, \color{red}{8}, \color{blue}{5}, \color{blue}{7}, \color{red}{9}]$.