Get out!
A square parking space for cars has a side length of meters. Cars of dimensions meters are parked in it. The cars are parked parallel to the sides of the square and at integer coordinates, some horizontally (along rows) and some vertically (along columns). Each car can only move along its axis and within the square, provided it is not obstructed by another car, and cannot rotate.
The natural gas supply company wants to dig along a horizontal line (row) at position inside the parking space to pass an underground pipeline. For this purpose, the cars parked on this line must be moved.
Problem:
Develop a program in one of the IOI languages which, after reading the length of the square's side, the position where the pipeline will be placed, and the positions of the parked cars, calculates the minimum number of moves required to clear the pipeline's row. In each move, a car is moved along its axis (forward or backward), one or more positions.
Input Files:
The input files named getout.in are text files with the following structure: The first line contains two integers: the length of the square's side and the position of the row where the pipeline will be placed . The next lines describe the cars parked horizontally along the rows of the square. Each line starts with the number , the number of cars parked along this row, followed by integers: the coordinates where the cars are parked along this row. (The coordinates are between and . A car parked horizontally with coordinate is located on columns and of the square.) Similarly, the next lines describe the cars parked vertically along the columns of the square.
Output Files:
The output files named getout.out are text files with only one line containing only one number: the minimum number of moves. If it is not possible to clear row to place the pipeline, the result will be .
Example of Input - Output Files:
STDIN (getout.in)
5 4
2 2 4
0
1 2
0
0
1 3
0
1 4
1 3
0
STDOUT (getout.out)
4
Explanation of the Example:
To free up row of the square, it is sufficient to move car Δ two positions down, then car Z one position down, then car Γ one position left, and finally car Ε two positions down. Therefore, moves are needed.
Formatting: In the output, each line terminates with a newline
character.
Maximum execution time: 1 sec.
Maximum available memory: 128 MB.
Comments