Calendar of Events

Time limit1sMemory limit128 MB

Summary
Given old and new schedules of N meetings, simulate prefix reversals that place each target day and list the request sizes.
Level

Medium4 of 10

Topics
Simulation, Implementation, Array, Sorting
Solved
No attempts yet

Problem

Marketing people are very creative and attend a great many meetings. The company's monthly schedule lists one meeting for each of the first NN days of the month. Every meeting is identified by a non-negative integer, and the same identifier may appear on several days.

The current schedule (the old order) must be turned into a chosen new order. The only operation the planning department may perform is a request: pick a number DD and reverse the meetings of the first DD days. For example, if the current order is 1 2 3 4 5, a request with D=3D = 3 turns it into 3 2 1 4 5.

Because an identifier may repeat, occurrences are matched by order: if a value is the kk-th occurrence from the left in the old order, that meeting must reach the position of the kk-th occurrence of the same value in the new order. This assigns to every day of the old schedule a unique target position in 1…N1 \ldots N.

The department always follows this fixed procedure. For size=N,N−1,…,2\text{size} = N, N-1, \ldots, 2 in this order:

  • Let pp be the current position (counting from the front) of the day whose target position is size\text{size}; this day is always among the first size\text{size} days.
  • If p=sizep = \text{size}, issue no request.
  • Otherwise, if p≠1p \neq 1, first issue the request pp (reverse the first pp days); then issue the request size\text{size} (reverse the first size\text{size} days).

Report every request the procedure issues, in order.

Input

The input contains several reorganizations; the last one is followed by a line containing a single zero.

Each reorganization consists of three lines. The first line is an integer NN (1≤N≤301 \le N \le 30), the number of days considered. The second line is the old schedule and the third line is the new schedule; each lists NN meeting identifiers (non-negative integers) separated by spaces. Both schedules contain the same multiset of identifiers, and some may appear more than once.

Output

For each reorganization, print one line with the request numbers D1 D2 … DkD_1\ D_2\ \ldots\ D_k issued by the procedure, separated by single spaces (1≤Di≤N1 \le D_i \le N). If the old and new schedules are already equal, the procedure issues no request; print an empty line.

Examples3

  1. Example 1

    Input
    5
    1 4 8 9 10
    4 8 9 10 1
    0
    
    Expected output
    5 4
    
  2. Example 2

    Input
    3
    1 2 3
    3 2 1
    0
    
    Expected output
    3
    
  3. Example 3

    Input
    2
    5 9
    9 5
    0
    
    Expected output
    2