CodeTON Round 9 (Div. 1 + Div. 2, Rated, Prizes!)
H2
Cool Swap Walk (Hard Version)
Codeforces
3500
原题
← H1→
翻译中…

This is the hard version of the problem. The only difference is the maximum number of operations you can perform. You can only make hacks if both versions are solved.

翻译中…

You are given an array $a$ of size $n$.

翻译中…

A cool swap walk is the following process:

翻译中…
  • In an $n \times n$ grid, we note the cells in row $i$ and column $j$ as $(i, j)$. You need to walk from $(1,1)$ to $(n,n)$, taking only steps to the right or down.
  • Formally, if you are in $(x,y)$ currently, you can step to either $(x+1,y)$ or $(x,y+1)$, but you can not step beyond the boundaries of the grid.
  • When you step in $(i,j)$, you must swap $a_i$ and $a_j$ when $i \neq j$.
翻译中…

You can perform at most $n+4$ cool swap walks. Sort the array $a_1, a_2, \ldots, a_n$ in non-decreasing order. We can show that it's always possible to do so.

翻译中…

Input

翻译中…

The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

翻译中…

The first line of each test case contains an integer $n$ ($2 \leq n \leq 500$) — the size of the array.

翻译中…

The second line of each test case contains $n$ integers $a_1,a_2,\ldots ,a_n$ ($1 \le a_i \le n$) — the elements of the array.

翻译中…

It is guaranteed that the sum of $n^2$ over all test cases does not exceed $2.5 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, your output should consist of several lines:

翻译中…
  • The first line contains an integer $k$ ($0 \leq k \leq n+4$), representing the number of cool swap walks you perform.
  • Each of the next $k$ lines contains a string $s$ of length $2n-2$ consisting only of R and D, representing the path (letters are case sensitive). For all $1 \le i \le 2n-2$, if $s_i=$ R, you walk right in the $i$-th step, otherwise you walk down in the $i$-th step.
翻译中…

Note

翻译中…

In the first test case, the array $a$ is already non-decreasing, so you don't need to perform any walk.

翻译中…

In the second test case, $a=[2,1,3]$ initially.

翻译中…

In the first walk:

翻译中…
  • In the $1$-st step, you step right to $(1,2)$. Then, $a=[1,2,3]$. Note that although the array $a$ is already non-decreasing, you can not stop until you reach $(n,n)$.
  • In the $2$-nd step, you step right to $(1,3)$. Then, $a=[3,2,1]$.
  • In the $3$-rd step, you step down to $(2,3)$. Then, $a=[3,1,2]$.
  • In the $4$-th step, you step down to $(3,3)$. Then, $a=[3,1,2]$.
翻译中…

In the second walk:

翻译中…
  • In the $1$-st step, you step down to $(2,1)$. Then, $a=[1,3,2]$.
  • In the $2$-nd step, you step right to $(2,2)$. Then, $a=[1,3,2]$.
  • In the $3$-rd step, you step down to $(3,2)$. Then, $a=[1,2,3]$.
  • In the $4$-th step, you step down to $(3,3)$. Then, $a=[1,2,3]$.
翻译中…

After the two cool swap walks above, we get $a=[1,2,3]$, which is non-decreasing.

翻译中…

样例

Input #1
3
2
1 2
3
2 1 3
4
3 2 3 4
Output #1
0
2
RRDD
DRDR
3
RRDRDD
DRDDRR
DDRRRD
InputCopy OutputCopy
题目信息
题号
2039H2
时间限制
2 s
内存限制
256 MB
标签
构造
模拟
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。