#P6892. Baggage Reordering

    ID: 20099 Type: Default 1000ms 256MiB

Baggage Reordering

Baggage Reordering

An airline has two flights departing simultaneously from ICPCity to city B and city A. There are \(n\) counters, each with a pair of identical baggage bins (one for city B and one for city A). Just before the flights depart, a motorized cart moves each pair of bins from a counter to a sorting area. In the sorting area the \(2n\) bins are lined up in order as follows:

\(B\ A\ B\ A\ \cdots\ B\ A\)

The bin locations are numbered from \(1\) to \(2n\) (from left to right). There are also \(2n\) initially empty positions immediately to the left of the bins, numbered from \(0\) to \(-2n+1\). The task is to reorder the bins so that all the bins for city A precede all the bins for city B. In other words, after reordering the \(2n\) bins must occupy consecutive positions (not necessarily starting at 1) so that the first \(n\) bins are all A and the last \(n\) bins are all B.

The reordering is accomplished by repeatedly moving a pair of adjacent bins (the bins in their current order are never separated) to an empty segment of at least two consecutive positions using the motorized cart. It can be shown that when \(n\ge 2\) a shortest sequence of moves is given by the following strategy:

  • For \(i=1\) to \(n-1\), move the pair starting at position \(2i+1\) (i.e. move bins from positions \(2i+1\) and \(2i+2\)).
  • Then move the pair starting at position \(2\).
  • Then for \(i=n-1\) downto \(1\), move the pair starting at position \(2i+1\) again.

Given the integer \(n\) (with \(n\ge 2\)), output the number of moves \(m=2n-1\) and the sequence of moves (i.e. the starting positions of the adjacent pair moved on each move) as described above.

inputFormat

The input consists of a single integer \(n\) (with \(n\ge 2\)).

outputFormat

On the first line, output an integer \(m\) representing the number of moves. On the second line, output \(m\) integers separated by spaces. These integers are the starting positions (in the current configuration) of the pair of adjacent bins being moved in each move.

The answer must follow the strategy below, which can be proved to be optimal and shortest:

  1. For \(i=1\) to \(n-1\), output \(2i+1\).
  2. Output \(2\).
  3. For \(i=n-1\) down to \(1\), output \(2i+1\).

sample

2
3

3 2 3

</p>