#P6447. Servant Reordering via Skipping Moves

    ID: 19661 Type: Default 1000ms 256MiB

Servant Reordering via Skipping Moves

Servant Reordering via Skipping Moves

The King has \(n\) servants arranged in a circle. The servants are numbered and listed in a sequence that always starts with servant number 1. Each servant faces the back of the next one in line. When a servant inserts himself in front of the servant immediately before him, it is considered a skipping move.

When the King announces a number \(b\), the servant with number \(b\) will move forward exactly \(b\) positions. Formally, if a servant with label \(b\) is currently at position \(i\) (using 0–indexed positions) and the move is allowed only if \(i \ge b\), then after the move the servant is removed from position \(i\) and inserted at position \(i - b\). (In the original description the positions are 1–indexed and the move is from position i to i - b.)

You are given an initial arrangement and a target arrangement (both are permutations of the servants). It is guaranteed that the two arrangements are different and that the target can be obtained by a sequence of valid moves. Your task is to determine the sequence of commands (i.e. the numbers announced by the King) so that applying them in order on the initial arrangement yields the target arrangement.

Note: When a command \(b\) is issued, the move is executed on the current arrangement. The ordering is maintained as a list in which, after each move, the list is reindexed from 0 (with servant 1 always at the start if possible – note that the circle is reoriented so that servant 1 is at the beginning).

Example:
For n=6 with initial sequence 1 5 4 3 2 6 and target sequence 1 5 2 4 3 6, the King only needs to announce 2 once. In the initial arrangement the servant with label 2 is at position 4 (0–indexed position 4 when counting from 0 in a 6–element list) and after moving forward 2 positions (i.e. from index 4 to index 2) the arrangement becomes the target sequence.

You need to output the sequence of commands as a space–separated list of numbers on one line.

inputFormat

The input consists of three lines:

  • The first line contains an integer n indicating the number of servants.
  • The second line contains n space-separated integers representing the initial arrangement.
  • The third line contains n space–separated integers representing the target arrangement.

outputFormat

Output a single line containing the King's commands – the sequence of numbers (each representing a command) separated by a single space. It is guaranteed that a sequence exists.

sample

6
1 5 4 3 2 6
1 5 2 4 3 6
2