rabbit house

### Problem

Barbara got really good grades in school last year, so her parents decided to gift her with a pet rabbit. She was so excited that she built a house for the rabbit, which can be seen as a 2D grid with $\mathbf{R}$$\mathbf{R}$ rows and $\mathbf{C}$$\mathbf{C}$ columns.

Rabbits love to jump, so Barbara stacked several boxes on several cells of the grid. Each box is a cube with equal dimensions, which match exactly the dimensions of a cell of the grid.

However, Barbara soon realizes that it may be dangerous for the rabbit to make jumps of height greater than $1$$1$ box, so she decides to avoid that by making some adjustments to the house. For every pair of adjacent cells, Barbara would like that their absolute difference in height be at most $1$$1$ box. Two cells are considered adjacent if they share a common side.

As all the boxes are superglued, Barbara cannot remove any boxes that are there initially, however she can add boxes on top of them. She can add as many boxes as she wants, to as many cells as she wants (which may be zero). Help hew determine what is the minimum total number of boxes to be added so that the rabbit's house is safe.

### Input

The first line of the input gives the number of test cases, $\mathbf{T}$$\mathbf{T}$. $\mathbf{T}$$\mathbf{T}$ test cases follow.

Each test case begins with a line containing two integers $\mathbf{R}$$\mathbf{R}$ and $\mathbf{C}$$\mathbf{C}$.

Then, $\mathbf{R}$$\mathbf{R}$ lines follow, each with $\mathbf{C}$$\mathbf{C}$ integers. The $j$$j$-th integer on $i$$i$-th line, ${\mathbf{G}}_{\mathbf{i}\mathbf{,}\mathbf{j}}$$\mathbf{G_{i,j}}$, represents how many boxes are there initially on the cell located at the $i$$i$-th row and $j$$j$-th column of the grid.

### Output

For each test case, output one line containing Case #x$x$$x$: y$y$$y$, where $x$$x$ is the test case number (starting from 1) and $y$$y$ is the minimum number of boxes to be added so that the rabbit's house is safe.

### Limits

Memory limit: 1 GB.
$1<=\mathbf{T}<=100$$1 \le \mathbf{T} \le 100$.
$0<={\mathbf{G}}_{\mathbf{i}\mathbf{,}\mathbf{j}}<=2\cdot {10}^{6}$$0 \le \mathbf{G\_{i,j}} \le 2 \cdot 10^6$, for all $i$$i$, $j$$j$.

#### Test Set 1

Time limit: 20 seconds.
$1<=\mathbf{R},\mathbf{C}<=50$$1 \le \mathbf{R}, \mathbf{C} \le 50$.

#### Test Set 2

Time limit: 40 seconds.
$1<=\mathbf{R},\mathbf{C}<=300$$1 \le \mathbf{R}, \mathbf{C} \le 300$.

In Sample Case #1, the absolute difference in height for every pair of adjacent cells is already at most $1$$1$ box, so there is no need to add any extra boxes.

In Sample Case #2, the absolute difference in height of the left-most cell and the middle cell is $3$$3$ boxes. To fix that, we can add $2$$2$ boxes to the middle cell. But then, the absolute difference of the middle cell and the right-most cell will be $2$$2$ boxes, so Barbara can fix that by adding $1$$1$ box to the right-most cell. After adding these $3$$3$ boxes, the safety condition is satisfied.

In Sample Case #3, the cell in the middle of the grid has an absolute difference in height of $2$$2$ boxes with all of its four adjacent cells. One solution is to add exactly $1$$1$ box to all of the middle's adjacent cells, so that the absolute difference between any pair of adjacent cells will be at most $1$$1$ box. That requires $4$$4$ boxes in total.

#### Input

3
1 3
3 4 3
1 3
3 0 0
3 3
0 0 0
0 2 0
0 0 0


#### Output

Case #1: 0
Case #2: 3
Case #3: 4

rabbit house
Analysis Barbara is given a 2D grid with R$\mathbf{R}$$\mathbf{R}$ rows and C$\mathbf{C}$$\mathbf{C}$ columns. Her goal is to create a grid such that no two adjacent cells have an absolute difference in height greater than 1$1$$1$. Moreover, she is only allowed to increase the height of cells. We notice that the cell initially with the largest height (H$H$$H$) never requires increasing. Furthermore, Barbara can update its neighbor cells to have a height of H−1$H-1$$H - 1$ (unless they already have a height of H$H$$H$). Afterwards, she can repeat the process for the cell with the next largest height, until she visits all the cells. One thing to be careful of in this problem is that the final result can be larger than the limits of a 32-bit integer. Using 64-bit integers avoids WAs due to overflow. Test set 1 For this test set, Barbara sees that 1<=R,C<=50$1<=\mathbf{R},\mathbf{C}<=50$$1 \le \mathbf{R}, \mathbf{C} \le 50$. Therefore, she performs a linear scan over the grid to find the cell with the largest height. Then, she updates the height of its neighbors, and increments the result to account for the increase in height. Finally, she marks this cell as visited, which can be done via a secondary 2D grid of booleans. She repeats the above process until all cells have been visited, and return the result. The linear scan can be done in O(R⋅C)$O\left(\mathbf{R}\cdot \mathbf{C}\right)$$O(\mathbf{R} \cdot \mathbf{C})$, and Barbara visits each cell exactly once, so she performs the linear scan O(R⋅C)$O\left(\mathbf{R}\cdot \mathbf{C}\right)$$O(\mathbf{R} \cdot \mathbf{C})$ times. The overall time complexity is therefore O((R⋅C)2)$O\left(\left(\mathbf{R}\cdot \mathbf{C}{\right)}^{2}\right)$$O((\mathbf{R} \cdot \mathbf{C})^2)$. This fits within the limits of the small test set. The additional space complexity is O(R⋅C)$O\left(\mathbf{R}\cdot \mathbf{C}\right)$$O(\mathbf{R} \cdot \mathbf{C})$ due to the secondary 2D grid of booleans. Test set 2 For this test set, 1<=R,C<=300$1<=\mathbf{R},\mathbf{C}<=300$$1 \le \mathbf{R}, \mathbf{C} \le 300$. A time complexity of O((R⋅C)2)$O\left(\left(\mathbf{R}\cdot \mathbf{C}{\right)}^{2}\right)$$O((\mathbf{R} \cdot \mathbf{C})^2)$ will not satisfy the time limits. Barbara still needs to visit the unvisited cell with the largest height in each iteration. However, this can be done in O(log(R⋅C))$O\left(\mathrm{log}\left(\mathbf{R}\cdot \mathbf{C}\right)\right)$$O(\log(\mathbf{R} \cdot \mathbf{C}))$ using a priority queue. In each iteration, she pops the cell with the largest height, updates the height of its neighbors in the priority queue, and increments the result. The time complexity of the above solution is O(R⋅C⋅log(R⋅C))$O\left(\mathbf{R}\cdot \mathbf{C}\cdot \mathrm{log}\left(\mathbf{R}\cdot \mathbf{C}\right)\right)$$O(\mathbf{R} \cdot \mathbf{C} \cdot \log(\mathbf{R} \cdot \mathbf{C}))$. The additional space complexity is still O(R⋅C)$O\left(\mathbf{R}\cdot \mathbf{C}\right)$$O(\mathbf{R} \cdot \mathbf{C})$, since initially the priority queue contains all the cells. Note that, depending on the implementation, updating a non-top element in the priority queue might not be an O(1)$O\left(1\right)$$O(1)$ operation. In such cases, one trick would be to insert a new element into the queue with the new height, and update the height in the grid. On processing any element, check whether the height in the queue corresponds to the height in the grid, and ignore the element otherwise. This does not change the worst-case time and space complexity, since the maximum number of times a cell can be added to the queue is 4$4$$4$. Further Improvements While not necessary to pass the time limits, the time complexity can be reduced further. One approach is to replace the priority queue with a list of buckets, each bucket containing a set of cells with a given height. With this approach, Barbara can iterate over the buckets in decreasing order of height in order to visit each cell, then apply the same algorithm as above: update the neighbors (by placing them in their new buckets according to their new height), and remove the visited cells from the list. By using a hashset for each bucket, insertion and removal becomes an O(1)$O\left(1\right)$$O(1)$ operation. She can also use a vector. However, the trick from Test Set 2 is needed to maintain an O(1)$O\left(1\right)$$O(1)$ insertion/removal in that case. Iterating over the buckets is linear in the number of buckets, and she will visit at most O(R⋅C)$O\left(\mathbf{R}\cdot \mathbf{C}\right)$$O(\mathbf{R} \cdot \mathbf{C})$ cells. Let G=max(Gi,j)$G=max\left({\mathbf{G}}_{\mathbf{i}\mathbf{,}\mathbf{j}}\right)$$G = \max(\mathbf{G_{i,j}})$. Since no cell has an initial height larger than G$G$$G$, and she will never increase the height of the cell with the largest height, she observes that the number of buckets is at most G$G$$G$. This leads to a time complexity of O(R⋅C+G)$O\left(\mathbf{R}\cdot \mathbf{C}+G\right)$$O(\mathbf{R} \cdot \mathbf{C} + G)$. Barbara can improve this further. Notice that all cells in the final grid will have a height of at least G−R−C+2$G-\mathbf{R}-\mathbf{C}+2$$G - \mathbf{R} - \mathbf{C} + 2$. She achieves this value by decreasing the height by 1 with each step away from the highest cell. The maximum number of steps occurs when the highest cell is in a corner of the grid: the opposite corner is R+C−2$\mathbf{R}+\mathbf{C}-2$$\mathbf{R} + \mathbf{C} - 2$ steps away. Now, she can first increase the height of all cells to G−R−C+2$G-\mathbf{R}-\mathbf{C}+2$$G - \mathbf{R} - \mathbf{C} + 2$, then apply the same bucketing approach. Except, now the number of buckets is R+C−2$\mathbf{R}+\mathbf{C}-2$$\mathbf{R} + \mathbf{C} - 2$, leading to a time complexity of O(R⋅C+R+C)=O(R⋅C)$O\left(\mathbf{R}\cdot \mathbf{C}+\mathbf{R}+\mathbf{C}\right)=O\left(\mathbf{R}\cdot \mathbf{C}\right)$$O(\mathbf{R} \cdot \mathbf{C} + \mathbf{R} + \mathbf{C}) = O(\mathbf{R} \cdot \mathbf{C})$. 
rabbit house
use rustgym_util::*;
use std::collections::BinaryHeap;
use std::fmt::Write;
use std::io::*;

fn solve(case_no: usize, reader: &mut impl BufRead, writer: &mut impl Write) {
let r = args;
let c = args;
let mut queue: BinaryHeap<(i64, usize, usize)> = BinaryHeap::new();
for i in 0..r {
for j in 0..c {
queue.push((a[i][j], i, j));
}
}
let mut b: Vec<Vec<i64>> = a.clone();
let mut res = 0;
while let Some((h, i, j)) = queue.pop() {
res += h - a[i][j];
if i > 0 && h - 1 > b[i - 1][j] {
b[i - 1][j] = h - 1;
queue.push((h - 1, i - 1, j));
}
if j > 0 && h - 1 > b[i][j - 1] {
b[i][j - 1] = h - 1;
queue.push((h - 1, i, j - 1));
}
if i + 1 < r && h - 1 > b[i + 1][j] {
b[i + 1][j] = h - 1;
queue.push((h - 1, i + 1, j));
}
if j + 1 < c && h - 1 > b[i][j + 1] {
b[i][j + 1] = h - 1;
queue.push((h - 1, i, j + 1));
}
}
writeln!(writer, "Case #{}: {}", case_no, res).unwrap();
}