Problem G
Claw Machine
Languages
en
sv
Olivia and Anton are at Liseberg, where they’ve found a claw machine with $N$ stuffed animals, numbered from $1$ to $N$. Olivia is highly skilled and wants to show off her abilities to Anton.
The two-dimensional claw machine can be modeled as a grid of size $R \times C$, with $R$ rows and $C$ columns. The stuffed animals are packed tightly, with each cell of the grid belonging to one stuffed animal. There are no empty cells in the grid. A stuffed animal may consist of several connected cells in the grid, forming a contiguous shape. In other words, you can draw each stuffed animal with a thick pen without lifting it from the paper.
In a single move, Olivia can choose a stuffed animal that does not have any other stuffed animal directly above it and lift it out of the claw machine. Since Olivia hasn’t asked Anton about his favorite animal yet, she wants to know, for each stuffed animal, how many moves she will need to make in total before she can lift it.
Input
The first line contains the integers $N$, $R$, and $C$ ($1 \leq N \leq R \times C \leq 4 \times 10^5$). The next $R$ lines describe the grid. The first line corresponds to the top of the claw machine, and the last line corresponds to the bottom. The $i$-th rows contains $C$ integers $a_{i,1}, a_{i,2}, ..., a_{i,C}$ ($1 \leq a_{i,j} \leq N$). Stuffed animal number $x$ consists of all cells $(i, j)$ such that $a_{i,j} = x$. It is guaranteed that each stuffed animal consists of at least one cell and forms a contiguous region.
Output
For each stuffed animal $1, 2, ..., N$, print the number of moves Olivia needs to make before she can lift that animal. If it is not possible, print $-1$.
Scoring
Your solution will be tested on a set of test groups, each worth a number of points. Each test group contains a set of test cases. To get the points for a test group you need to solve all test cases in the test group.
Group |
Points |
Constraints |
$1$ |
$10$ |
$1 \leq N \leq R \times C \leq 20$ |
$2$ |
$5$ |
$C = 1$ |
$3$ |
$10$ |
$C \leq 2$ |
$4$ |
$15$ |
Each stuffed animal has a rectangular shape. |
$5$ |
$15$ |
$N \leq 2000$ |
$6$ |
$7$ |
$N \leq 50000$ |
$7$ |
$38$ |
No additional constraints. |
Explanation of Sample 1
In sample 1, the claw machine looks like this:
To lift stuffed animal $1$, no other animal needs to be lifted first, so the answer is $0$. To lift stuffed animal $2$, Olivia first needs to lift $3$, $4$, $6$, and $7$, so the answer is $4$. To lift stuffed animal $3$, no other animal needs to be lifted first. To lift stuffed animal $4$, Olivia needs to lift $3$ and $6$ first, so the answer is $2$. The answers for stuffed animals $5$, $6$, and $7$ are $3$, $0$, and $3$, respectively.
Here is how it looks after lifting stuffed animal $3$:
Explanation of Sample 4
Neither stuffed animal $1$ nor $2$ has anything above them, so the answers for both are $0$. To lift stuffed animal $3$, Olivia first needs to lift $4$, but to lift $4$, she needs to lift $3$, creating a loop. Thus, the answer for both $3$ and $4$ is $-1$, as it is impossible to lift them.
Sample Input 1 | Sample Output 1 |
---|---|
7 4 4 1 3 3 6 1 4 4 4 1 7 4 5 1 2 4 5 |
0 4 0 2 3 0 3 |
Sample Input 2 | Sample Output 2 |
---|---|
4 5 1 2 1 1 3 4 |
1 0 2 3 |
Sample Input 3 | Sample Output 3 |
---|---|
5 4 5 1 1 2 2 3 1 1 2 2 3 4 2 2 2 3 4 2 5 2 3 |
0 1 0 1 2 |
Sample Input 4 | Sample Output 4 |
---|---|
4 4 4 1 1 2 2 3 3 3 2 3 4 3 2 3 3 3 2 |
0 0 -1 -1 |