[The explanations of the algorithm come from here. I recommend reading it for a beautiful description of the algorithm.]
This challenge is to implement the Robinson Schensted correspondence.
Input
A permutation \$\pi\$ of the integers \$\{1, \dots, n\}\$.
Output
A pair of Young Tableaux corresponding to the Robinson Schensted correspondence of \$\pi\$.
Explanation
A Young Tableau of size \$n\$ is a two-dimensional arrangement of the numbers from \$1\$ to \$n\$, in which each number appears exactly once. They are arranged in left-justified rows, one below the other, and no row has more entries than the one above it. Also, the entries in each row are in increasing order as you read them from left to right, and the entries in each column are in increasing order as you read them from top to bottom. citation
The Robinson-Schensted algorithm takes a permutation of the numbers \$1\$ to \$n\$ and produces two Young Tableaux from it. We call these two tableaux \$P\$ and \$Q\$.
I will describe how to make \$P\$ first.
Given a permutation \$a_1, \dots, a_n\$, we process the numbers in the permutation from left to right. We try to insert them one after another into a growing Young tableau, making new rows where we can't do it while satisfying the conditions that the entries in each row are in increasing order as you read them from left to right.
We start with \$a_1\$ and place it in the top left. We then try to place \$a_2\$ to its right. If \$a_2 < a_1\$ then we make a new row, place \$a_1\$ in the new row on the left and place \$a_2\$ in the top left. If \$a_2 > a_1\$ then we simply append it to the first row. Now consider \$a_3\$. If it is larger than all the numbers in the first row we simply append it to the first row. If not, we find the first number in the first row that is larger than it and replace that number with \$a_3\$. We now take the number that we replaced, and we insert it into the second row, in the same way as before. This may cause a number to be "bumped" out of the second row and inserted into the third row. We keep going in the same way all through the permutation, always inserting the next number from our permutation starting at the first row of the tableau.
\$Q\$ has the same shape as \$P\$, but we just number the boxes by the step at which that box appeared in \$P\$.
Examples
pi = [2 1 3]
P = [[1, 3], [2]]
Q = [[1, 3], [2]]
pi = [1 3 2 4]
P = [[1, 2, 4], [3]]
Q = [[1, 2, 4], [3]]
pi = [5 3 4 1 2]
P = [[1, 2], [3, 4], [5]]
Q = [[1, 3], [2, 5], [4]]
pi = [1 5 6 4 2 3]
P = [[1, 2, 3], [4, 6], [5]]
Q = [[1, 2, 3], [4, 6], [5]]
pi = [4 2 6 5 7 3 1]
P = [[1, 3, 7], [2, 5], [4], [6]]
Q = [[1, 3, 5], [2, 4], [6], [7]]
1 3 7 _ _ _ _│2 5 _ _│4 _│6
rather than1 3 7 │2 5 │4 │6
. \$\endgroup\$