2118B.md (2136B)
1 Source: https://codeforces.com/problemset/problem/2118/B 2 3 B. Make It Permutation 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 There is a matrix $A$ of size $n\times n$ where $A_{i,j}=j$ for all $1 \le i,j \le n$. 22 23 In one operation, you can select a row and reverse any subarray$^{\text{∗}}$ in it. 24 25 Find a sequence of at most $2n$ operations such that every column will contain a permutation$^{\text{†}}$ of length $n$. 26 27 It can be proven that the construction is always possible. If there are multiple solutions, output any of them. 28 29 $^{\text{∗}}$An array $a$ is a subarray of an array $b$ if $a$ can be obtained from $b$ by deleting zero or more elements from the beginning and zero or more elements from the end. 30 31 $^{\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). 32 33 Input 34 35 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. 36 37 The first line of each test case contains one integer $n$ ($3 \le n \le 5000$) — denoting the number of rows and columns in the matrix. 38 39 It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$. 40 41 Output 42 43 For each test case, on the first line, print an integer $k$ $(0 \le k \le 2n)$, the number of operations you wish to perform. On the next lines, you should print the operations. 44 45 To print an operation, use the format "$i\;l\;r$" ($1 \leq l \leq r \leq n$ and $1 \leq i \leq n$) which reverses the subarray $A_{i, l}$, $A_{i, l+1}$, $\ldots$, $A_{i, r}$. 46 47 Example 48 49 Input 50 51 ``` 52 2 53 54 3 55 56 4 57 ``` 58 59 Output 60 61 ``` 62 4 63 2 1 3 64 2 2 3 65 3 1 2 66 3 2 3 67 5 68 2 1 4 69 3 1 3 70 3 2 4 71 4 3 4 72 4 1 2 73 ``` 74 75 Note 76 77 In the first test case, the following operations are a valid solution: 78 79 