Depot
Time limit1sMemory limit128 MB
Given the final row placement produced by the depot insertion rule, count how many arrival orders of the containers could have produced it.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Combinatorics, Implementation, Brute force
- Solved
- No attempts yet
Problem
A Finnish high-technology company has a big rectangular depot. The depot has a worker and a manager. The sides of the depot, in order around it, are called left, top, right, and bottom. The depot floor is divided into equal-sized square cells by splitting it into rows and columns. The rows are numbered from the top with integers 1, 2, ..., and the columns are numbered from the left with integers 1, 2, ...
The depot holds containers that store invaluable technological devices. Each container has a distinct identification number and occupies one cell. The depot is so big that the total number of containers that will ever arrive is smaller than the number of rows and smaller than the number of columns. Containers are never removed from the depot, but from time to time a new container arrives. The entrance to the depot is at the top-left corner.
The worker arranges the containers near the top-left corner so that he can find them by their identification numbers, using the following method.
Let be the identification number of the next container to be inserted (container , for short). The worker scans the first row from the left and looks for the first container whose identification number is larger than . If no such container is found, container is placed immediately after the rightmost container already in that row. If such a container is found, container is replaced by container , and the displaced container is inserted into the next row using the same method. If the worker reaches a row that has no containers, the container is placed in the leftmost cell of that row.
Suppose containers 3, 4, 9, 2, 5, 1 arrived at the depot in this order. Then the placement of the containers is as follows.
1 4 5
2 9
3
The manager comes to the worker and they have the following dialogue.
Manager: Did container 5 arrive before container 4?
Worker: No, that is impossible.
Manager: Oh, so you can tell the arrival order of the containers from their placement.
Worker: Generally not. For instance, the containers now in the depot could have arrived in the order 3, 2, 1, 4, 9, 5, or in the order 3, 2, 1, 9, 4, 5, or in one of 14 other orders.
The manager, not wanting the worker to seem much smarter than he is, walks away. You are to help the manager: write a program that, given a container placement, computes how many arrival orders could have produced it.
Input
The first line contains one integer , the number of rows that contain containers. The following lines describe rows from the top. Each such line begins with an integer , the number of containers in that row, followed by integers: the identification numbers of the containers in that row from the left. Every container identification number satisfies . If is the total number of containers in the depot, then .
Output
Print a single integer: the number of distinct arrival orders that could have produced the given placement.