Choreography
Time limit2sMemory limit512 MB
Members on non-overlapping equal intervals move one at a time to an intersecting interval, and the task is to reach set E from set S in the fewest steps.
- Level
Hard8 of 10
- Topics
- Greedy, Shortest path, Intervals
- Solved
- No attempts yet
Problem
Korean idol groups are known around the world for dynamic and elaborate group dances, called Kalgunmu. A group has members. One day the members practice their choreography on a stage. The stage is simplified as a line, and closed intervals of identical length are placed on it. During the choreography, the following rules must hold at every moment.
- R1) Each member stands on one of the intervals.
- R2) No two or more members can stand on the same interval.
- R3) Two intervals on which any two members stand cannot intersect.
Two closed intervals and with intersect if .
The choreography consists of several steps with the following rules.
- R4) At any step, only one member can move from her or his current interval to another interval. Two or more members cannot move at the same time.
- R5) When a member changes the interval she or he stands on, the member has to move into one of the intervals that the current interval intersects. A member cannot move into an interval if intersects an interval on which another member stands, according to R3.
- R6) The set of intervals on which the members stand at the start is .
- R7) The set of intervals on which the members stand at the end is .
The sets and are given in the input. Each has size , and the intervals in each set do not intersect each other. Each member moves from an interval in to an interval in during the choreography. Find a choreography from to that satisfies R1 to R7 with the minimum number of steps, and output every step.
In the first figure, six intervals are numbered 1 to 6. The blue intervals 1 and 4 are the starting set , and the red intervals 3 and 6 are the ending set .

Figure E.1 Initial configuration .

Figure E.2 Four steps of a choreography. The member on interval 4 moves to interval 6 in one step, which allows the whole choreography to finish in three steps.
Input
The first line contains two integers and (). The intervals are numbered 1 to , and the left endpoint of interval is strictly less than the left endpoint of interval . The second line contains the length of the intervals (). The third line contains integers in ascending order, where the -th number is the left endpoint of interval . These values are between 0 and . The next line contains the interval numbers of in ascending order. The last line contains the interval numbers of in ascending order.
Output
On the first line, print the minimum number of steps . On each of the following lines, print two integers and , meaning that at step a member moves from interval to interval . If several solutions exist, print any one of them. If no solution exists, print .