#P9698. [GDCPC2023] Path Planning

[GDCPC2023] Path Planning

题目描述

There is a grid with nn rows and mm columns. Each cell of the grid has an integer in it, where ai,ja_{i, j} indicates the integer in the cell located at the ii-th row and the jj-th column. Each integer from 00 to (n×m1)(n \times m - 1) (both inclusive) appears exactly once in the grid.

Let (i,j)(i, j) be the cell located at the ii-th row and the jj-th column. You now start from (1,1)(1, 1) and need to reach (n,m)(n, m). When you are in cell (i,j)(i, j), you can either move to its right cell (i,j+1)(i, j + 1) if j<mj < m or move to its bottom cell (i+1,j)(i + 1, j) if i<ni < n.

Let S\mathbb{S} be the set consisting of integers in each cell on your path, including a1,1a_{1, 1} and an,ma_{n, m}. Let mex(S)\text{mex}(\mathbb{S}) be the smallest non-negative integer which does not belong to S\mathbb{S}. Find a path to maximize mex(S)\text{mex}(\mathbb{S}) and calculate this maximum possible value.

输入格式

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains two integers nn and mm (1n,m1061 \le n, m \le 10^6, 1n×m1061 \le n \times m \le 10^6) indicating the number of rows and columns of the grid.

For the following nn lines, the ii-th line contains mm integers ai,1,ai,2,,ai,ma_{i, 1}, a_{i, 2}, \cdots, a_{i, m} (0ai,j<n×m0 \le a_{i, j} < n \times m) where ai,ja_{i, j} indicates the integer in cell (i,j)(i, j). Each integer from 00 to (n×m1)(n \times m - 1) (both inclusive) appears exactly once in the grid.

It's guaranteed that the sum of n×mn \times m of all test cases will not exceed 10610^6.

输出格式

For each test case output one line containing one integer indicating the maximum possible value of mex(S)\text{mex}(\mathbb{S}).

题目大意

【题目描述】

有一个 nnmm 列的网格。网格里的每个格子都写着一个整数,其中第 ii 行第 jj 列的格子里写着整数 ai,ja_{i, j}。从 00(n×m1)(n \times m - 1) 的每个整数(含两端)在网格里都恰好出现一次。

(i,j)(i, j) 表示位于第 ii 行第 jj 列的格子。您现在需要从 (1,1)(1, 1) 出发并前往 (n,m)(n, m)。当您位于格子 (i,j)(i, j) 时,您可以选择走到右方的格子 (i,j+1)(i, j + 1)(若 j<mj < m),也可以选择走到下方的格子 (i+1,j)(i + 1, j)(若 i<ni < n)。

S\mathbb{S} 表示路径上每个格子里的整数形成的集合,包括 a1,1a_{1, 1}an,ma_{n, m}。令 mex(S)\text{mex}(\mathbb{S}) 表示不属于 S\mathbb{S} 的最小非负整数。请找出一条路径以最大化 mex(S)\text{mex}(\mathbb{S}),并求出这个最大的值。

【输入格式】

有多组测试数据。第一行输入一个整数 TT 表示测试数据组数。对于每组测试数据:

第一行输入两个整数 nnmm1n,m1061 \le n, m \le 10^61n×m1061 \le n \times m \le 10^6)表示网格的行数和列数。

对于接下来 nn 行,第 ii 行输入 mm 个整数 ai,1,ai,2,,ai,ma_{i, 1}, a_{i, 2}, \cdots, a_{i, m}0ai,j<n×m0 \le a_{i, j} < n \times m),其中 ai,ja_{i, j} 表示格子 (i,j)(i, j) 里的整数。从 00(n×m1)(n \times m - 1) 的每个整数(含两端)在网格里都恰好出现一次。

保证所有数据 n×mn \times m 之和不超过 10610^6

【输出格式】

每组数据输出一行一个整数,表示最大的 mex(S)\text{mex}(\mathbb{S})

【样例解释】

对于第一组样例数据,共有 33 条可能的路径。

  • 第一条路径为 (1,1)(1,2)(1,3)(2,3)(1, 1) \to (1, 2) \to (1, 3) \to (2, 3)S={1,2,4,5}\mathbb{S} = \{1, 2, 4, 5\} 因此 mex(S)=0\text{mex}(\mathbb{S}) = 0
  • 第二条路径为 (1,1)(1,2)(2,2)(2,3)(1, 1) \to (1, 2) \to (2, 2) \to (2, 3)S={1,2,0,5}\mathbb{S} = \{1, 2, 0, 5\} 因此 mex(S)=3\text{mex}(\mathbb{S}) = 3
  • 第三条路径为 (1,1)(2,1)(2,2)(2,3)(1, 1) \to (2, 1) \to (2, 2) \to (2, 3)S={1,3,0,5}\mathbb{S} = \{1, 3, 0, 5\} 因此 mex(S)=2\text{mex}(\mathbb{S}) = 2

因此答案为 33

对于第二组样例数据,只有 11 条可能的路径,即 (1,1)(1,2)(1,3)(1,4)(1,5)(1, 1) \to (1, 2) \to (1, 3) \to (1, 4) \to (1, 5)S={1,3,0,4,2}\mathbb{S} = \{1, 3, 0, 4, 2\} 因此 mex(S)=5\text{mex}(\mathbb{S}) = 5

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

提示

For the first sample test case there are 33 possible paths.

  • The first path is (1,1)(1,2)(1,3)(2,3)(1, 1) \to (1, 2) \to (1, 3) \to (2, 3). S={1,2,4,5}\mathbb{S} = \{1, 2, 4, 5\} so mex(S)=0\text{mex}(\mathbb{S}) = 0.
  • The second path is (1,1)(1,2)(2,2)(2,3)(1, 1) \to (1, 2) \to (2, 2) \to (2, 3). S={1,2,0,5}\mathbb{S} = \{1, 2, 0, 5\} so mex(S)=3\text{mex}(\mathbb{S}) = 3.
  • The third path is (1,1)(2,1)(2,2)(2,3)(1, 1) \to (2, 1) \to (2, 2) \to (2, 3). S={1,3,0,5}\mathbb{S} = \{1, 3, 0, 5\} so mex(S)=2\text{mex}(\mathbb{S}) = 2.

So the answer is 33.

For the second sample test case there is only 11 possible path, which is (1,1)(1,2)(1,3)(1,4)(1,5)(1, 1) \to (1, 2) \to (1, 3) \to (1, 4) \to (1, 5). S={1,3,0,4,2}\mathbb{S} = \{1, 3, 0, 4, 2\} so mex(S)=5\text{mex}(\mathbb{S}) = 5.