Cow Photography
Time limit1sMemory limit128 MB
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 cows has a unique integer ID. He wants a picture of the cows standing in one specific order, given by an array , where is the ID of the -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 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 .
Each photograph shows the cows in an order that differs from 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 : the number of cows ().
- Lines : five orderings, each given as a block of consecutive lines. Each line holds one cow's ID, an integer between and inclusive.
Output
- Lines : the intended ordering , 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 is therefore 10, 20, 30, 40, 50.