Universal Cup Judging System

Universal Cup

Süre Sınırı: 3 s Bellek Sınırı: 1024 MB Toplam puan: 100
İstatistikler

$n$ 行 $m$ 列のグリッドがあります。各マスには整数が一つ入っており、$a_{i,j}$ は第 $i$ 行第 $j$ 列のマスにある整数を表します。$0$ から $(n\times m-1)$ までの各整数(両端を含む)は、グリッドにちょうど一度ずつ現れます。

$(i,j)$ を第 $i$ 行第 $j$ 列のマスとします。今、$(1,1)$ から出発して $(n,m)$ に到達する必要があります。マス $(i,j)$ にいるとき、$j<m$ なら右のマス $(i,j+1)$ へ、または $i<n$ なら下のマス $(i+1,j)$ へ移動できます。

$S$ を、経路上の各マスにある整数からなる集合とします。$a_{1,1}$ と $a_{n,m}$ も含まれます。$\operatorname{mex}(S)$ を、$S$ に属さない最小の非負整数とします。$\operatorname{mex}(S)$ を最大化する経路を見つけ、その取り得る最大値を求めてください。

入力

テストケースは複数あります。入力の最初の行には、テストケース数を表す整数 $T$ が含まれます。各テストケースについて:

最初の行には二つの整数 $n$ と $m$($1\le n,m\le10^6$、$1\le n\times m\le10^6$)が含まれ、グリッドの行数と列数を表します。

続く $n$ 行のうち第 $i$ 行には $m$ 個の整数 $a_{i,1},a_{i,2},\cdots,a_{i,m}$($0\le a_{i,j}<n\times m$)が含まれます。$a_{i,j}$ はマス $(i,j)$ の整数を表します。$0$ から $(n\times m-1)$ までの各整数(両端を含む)は、グリッドにちょうど一度ずつ現れます。

すべてのテストケースの $n\times m$ の総和は $10^6$ 以下であることが保証されます。

出力

各テストケースについて、$\operatorname{mex}(S)$ の取り得る最大値を表す整数を一行に一つ出力してください。

入出力例

入力 1

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

出力 1

3
5

注記

最初のサンプルテストケースには、可能な経路が 3 本あります。

  • 一つ目の経路は $(1,1)\to(1,2)\to(1,3)\to(2,3)$ です。$S=\{1,2,4,5\}$ なので $\operatorname{mex}(S)=0$ です。
  • 二つ目の経路は $(1,1)\to(1,2)\to(2,2)\to(2,3)$ です。$S=\{1,2,0,5\}$ なので $\operatorname{mex}(S)=3$ です。
  • 三つ目の経路は $(1,1)\to(2,1)\to(2,2)\to(2,3)$ です。$S=\{1,3,0,5\}$ なので $\operatorname{mex}(S)=2$ です。

したがって答えは 3 です。

二つ目のサンプルテストケースには可能な経路が 1 本だけあり、それは $(1,1)\to(1,2)\to(1,3)\to(1,4)\to(1,5)$ です。$S=\{1,3,0,4,2\}$ なので $\operatorname{mex}(S)=5$ です。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.