Cow Photography

Time limit1sMemory limit128 MB

Summary
Given five orderings of N cows where each cow moves in at most one photo, reconstruct the original intended order.
Level

Medium7 of 10

Topics
Sorting, Implementation, Math, Hash map
Solved
No attempts yet

Problem

The cows are in a particularly mischievous mood today. All Farmer John wants is to take a photograph of the cows standing in a line, but they keep moving right before he can snap the picture.

Each of Farmer John's NN cows has a unique integer ID. He wants a picture of the cows standing in one specific order, given by an array A[1…N]A[1 \dots N], where A[j]A[j] is the ID of the jj-th cow in that order. He arranges the cows in this order, but just before he presses the shutter, a group of zero or more cows (not necessarily contiguous) steps out of the line; the remaining cows shift over to close the gaps, and the cows that stepped out re-insert themselves at new positions (not necessarily where they originally stood). Frustrated but undeterred, Farmer John arranges the cows into order AA again, and again, right before he can snap the picture, a possibly different group of zero or more cows moves.

This process repeats for a total of five photographs before Farmer John gives up. Given the five photographs, reconstruct the intended order AA.

Each photograph shows the cows in an order that differs from AA in that some group of zero or more cows has moved. Crucially, each cow moves in at most one photograph: if a cow is part of the group that actively moves in one photo, she does not actively move in any of the other four (though she may still end up at a different index as a consequence of other cows around her moving).

Input

  • Line 11: the number of cows NN (1≤N≤200001 \le N \le 20000).
  • Lines 2…5N+12 \dots 5N+1: five orderings, each given as a block of NN consecutive lines. Each line holds one cow's ID, an integer between 00 and 1,000,000,0001{,}000{,}000{,}000 inclusive.

Output

  • Lines 1…N1 \dots N: the intended ordering AA, one ID per line.

Hint

In the first example there are 5 cows with IDs 10, 20, 30, 40, and 50. In each of the five photographs a different cow moves to the front of the line (here at most one cow moves per photograph, but in general several cows may move in a single photograph). The intended ordering AA is therefore 10, 20, 30, 40, 50.

Examples5

  1. Example 1

    Input
    5
    10
    20
    30
    40
    50
    20
    10
    30
    40
    50
    30
    10
    20
    40
    50
    40
    10
    20
    30
    50
    50
    10
    20
    30
    40
    
    Expected output
    10
    20
    30
    40
    50
    
  2. Example 2

    Input
    1
    777
    777
    777
    777
    777
    
    Expected output
    777
    
  3. Example 3

    Input
    2
    100
    200
    100
    200
    100
    200
    100
    200
    100
    200
    
    Expected output
    100
    200
    
  4. Example 4

    Input
    2
    5
    7
    7
    5
    5
    7
    5
    7
    5
    7
    
    Expected output
    5
    7
    
  5. Example 5

    Input
    3
    0
    500
    1000000000
    0
    500
    1000000000
    0
    500
    1000000000
    0
    500
    1000000000
    0
    500
    1000000000
    
    Expected output
    0
    500
    1000000000