Complete the columns from left to right. When processing column , consider its two target values, and , together. Never touch an already completed column.
Store the current position of every value and update it after each swap. Record the swaps, then print their count and the recorded sequence. Both the time complexity and the space complexity, including the recorded swaps, are .
Proof
Before column is processed, both targets are in columns through . All swaps remain within this region. The targets are placed in different rows before being moved left, so moving either target cannot displace the other. Thus column is completed correctly without disturbing earlier columns.
Let be the number of remaining columns. Each target needs at most horizontal swaps, and either case uses at most one vertical swap. The total number of swaps is therefore at most
which meets the full-score requirement.