Shifting Sort
Contest: Codeforces Round 744 (Div. 3)
Difficulty rating: 1100
Tags: implementation, sortings

The new generation external memory contains an array of integers $$$a[1 \ldots n] = [a_1, a_2, \ldots, a_n]$$$.

This type of memory does not support changing the value of an arbitrary element. Instead, it allows you to cut out any segment of the given array, cyclically shift (rotate) it by any offset and insert it back into the same place.

Technically, each cyclic shift consists of two consecutive actions:

1. You may select arbitrary indices $$$l$$$ and $$$r$$$ ($$$1 \le l < r \le n$$$) as the boundaries of the segment.
2. Then you replace the segment $$$a[l \ldots r]$$$ with it's cyclic shift to the left by an arbitrary offset $$$d$$$. The concept of a cyclic shift can be also explained by following relations: the sequence $$$[1, 4, 1, 3]$$$ is a cyclic shift of the sequence $$$[3, 1, 4, 1]$$$ to the left by the offset $$$1$$$ and the sequence $$$[4, 1, 3, 1]$$$ is a cyclic shift of the sequence $$$[3, 1, 4, 1]$$$ to the left by the offset $$$2$$$.

For example, if $$$a = [1, \color{blue}{3, 2, 8}, 5]$$$, then choosing $$$l = 2$$$, $$$r = 4$$$ and $$$d = 2$$$ yields a segment $$$a[2 \ldots 4] = [3, 2, 8]$$$. This segment is then shifted by the offset $$$d = 2$$$ to the left, and you get a segment $$$[8, 3, 2]$$$ which then takes the place of of the original elements of the segment. In the end you get $$$a = [1, \color{blue}{8, 3, 2}, 5]$$$.

Sort the given array $$$a$$$ using no more than $$$n$$$ cyclic shifts of any of its segments. Note that you don't need to minimize the number of cyclic shifts. Any method that requires $$$n$$$ or less cyclic shifts will be accepted.

Input format:
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases.

The next $$$2t$$$ lines contain the descriptions of the test cases.

The first line of each test case description contains an integer $$$n$$$ ($$$2 \leq n \leq 50$$$) — the length of the array. The second line consists of space-separated elements of the array $$$a_i$$$ ($$$-10^9 \leq a_i \leq 10^9$$$). Elements of array $$$a$$$ may repeat and don't have to be unique.

Output format:
Print $$$t$$$ answers to all input test cases.

The first line of the answer of each test case should contain an integer $$$k$$$ ($$$0 \le k \le n$$$) — the number of actions to sort the array. The next $$$k$$$ lines should contain descriptions of the actions formatted as "$$$l$$$ $$$r$$$ $$$d$$$" (without quotes) where $$$l$$$ and $$$r$$$ ($$$1 \le l < r \le n$$$) are the boundaries of the segment being shifted, while $$$d$$$ ($$$1 \le d \le r - l$$$) is the offset value. Please remember that only the cyclic shifts to the left are considered so the chosen segment will be shifted by the offset $$$d$$$ to the to the left.

Note that you are not required to find the minimum number of cyclic shifts needed for sorting. Any sorting method where the number of shifts does not exceed $$$n$$$ will be accepted.

If the given array $$$a$$$ is already sorted, one of the possible answers is $$$k = 0$$$ and an empty sequence of cyclic shifts.

If there are several possible answers, you may print any of them.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
Explanation of the fourth data set in the example:

1. The segment $$$a[2 \ldots 4]$$$ is selected and is shifted to the left by $$$2$$$: $$$[2, \color{blue}{5, 1, 4}, 3] \longrightarrow [2, \color{blue}{4, 5, 1}, 3]$$$
2. The segment $$$a[1 \ldots 5]$$$ is then selected and is shifted to the left by $$$3$$$: $$$[\color{blue}{2, 4, 5, 1, 3}] \longrightarrow [\color{blue}{1, 3, 2, 4, 5}]$$$
3. After that the segment $$$a[1 \ldots 2]$$$ is selected and is shifted to the left by $$$1$$$: $$$[\color{blue}{1, 3}, 2, 4, 5] \longrightarrow [\color{blue}{3, 1}, 2, 4, 5]$$$
4. And in the end the segment $$$a[1 \ldots 3]$$$ is selected and is shifted to the left by $$$1$$$: $$$[\color{blue}{3, 1, 2}, 4, 5] \longrightarrow [\color{blue}{1, 2, 3}, 4, 5]$$$

Example input:
4
2
2 1
3
1 2 1
4
2 4 1 3
5
2 5 1 4 3

Example output:
1
1 2 1
1
1 3 2
3
2 4 1
2 3 1
1 3 2
4
2 4 2
1 5 3
1 2 1
1 3 1

Editorial:
In this problem, it was enough to implement an analogue of standard selection sort or insertion sort. Here is an example of a solution based on selection sort.
Let's find the minimum element in the array by simply iterating over it. Let's denote its index in the array by $$$p_1$$$. If we apply a shift "$$$1$$$ $$$p_1$$$ $$$(p_1 - 1)$$$" to it, the following happens: $$$$$$a \rightarrow [\color{blue}{a_\color{red}{p_1}, a_1, a_2, \ldots, a_{p_1 - 1}}, a_{p_1 + 1}, \ldots, a_n]$$$$$$ Once again, please note that all shifts are made in the left direction, so shifting by $$$p_1 - 1$$$ to the left will put the element that was previously at index $$$p_1$$$ in the first place.
Let us perform a similar shift for the second-largest element of the array, putting it in second place, for the third-largest element of the array, putting it in third place, and so on. More formally, let's describe the $$$i$$$-th iteration as follows:
1. At the beginning of the $$$i$$$-th iteration, the first $$$i - 1$$$ elements of the array are its $$$i - 1$$$ minimal elements, already in their correct places in sorted order.
2. During the $$$i$$$-th iteration, the $$$i$$$-th largest element of the array is placed in the $$$i$$$-th place in the array.
3. Since the first $$$i - 1$$$ minimal elements are already in their places, the $$$i$$$-th largest element of the array is simply the smallest element among $$$[a_i, a_{i + 1}, \ldots, a_n]$$$. Let's find it by iterating over these elements and denote its index in the array $$$a$$$ by $$$p_i$$$.
4. Make a shift "$$$i$$$ $$$p_i$$$ $$$(p_i - i)$$$". The first $$$i - 1$$$ elements will not change, and the element from the $$$p_i$$$-th place in the array will move to the $$$i$$$-th: $$$$$$a \rightarrow [a_1, \ldots, a_{i - 1}, \color{blue}{a_\color{red}{p_i}, a_i, a_{i + 1}, \ldots, a_{p_i - 1}}, a_{p_i + 1}, \ldots, a_n]$$$$$$ It is worth noting that the output format forbids shifting segments with $$$l = r$$$. Regarding this case, we should check the equality $$$i = p_i$$$ separately. If these two indexes coincide, then the $$$i$$$-th element is already in its place, and no shift should be done on this iteration.
Let us repeat this algorithm for $$$i = 2$$$, $$$i = 3$$$, ..., $$$i = n - 1$$$. At each iteration, the new element will be shifted into its place in sorted order, and each iteration performs no more than one shift operation. Thus, in strictly less than $$$n$$$ shifts, the array will be completely sorted.
The time complexity is $$$\mathcal{O}\left(t \cdot n^2\right)$$$.