Good Bye 2024: 2025 is NEAR
F
Earnest Matrix Complement
Codeforces
2600
原题
← EG →
翻译中…

3, 2, 1, ... We are the — RiOI Team!— Felix & All, Special Thanks 3

翻译中…
  • Peter: Good news: My problem T311013 is approved!
  • $\delta$: I'm glad my computer had gone out of battery so that I wouldn't have participated in wyrqwq's round and gained a negative delta.
  • Felix: [thumbs_up] The problem statement concerning a removed song!
  • Aquawave: Do I mourn my Chemistry?
  • E.Space: ahh?
  • Trine: Bread.
  • Iris: So why am I always testing problems?
翻译中…

Time will pass, and we might meet again. Looking back at the past, everybody has lived the life they wanted.

翻译中…

Aquawave has a matrix $A$ of size $n\times m$, whose elements can only be integers in the range $[1, k]$, inclusive. In the matrix, some cells are already filled with an integer, while the rest are currently not filled, denoted by $-1$.

翻译中…

You are going to fill in all the unfilled places in $A$. After that, let $c_{u,i}$ be the number of occurrences of element $u$ in the $i$-th row. Aquawave defines the beauty of the matrix as

翻译中…

$$\sum_{u=1}^k \sum_{i=1}^{n-1} c_{u,i} \cdot c_{u,i+1}.$$

翻译中…

You have to find the maximum possible beauty of $A$ after filling in the blanks optimally.

翻译中…

Input

翻译中…

The first line of input contains a single integer $t$ ($1 \leq t \leq 2\cdot 10^4$) — the number of test cases. The description of test cases follows.

翻译中…

The first line of each test case contains three integers $n$, $m$, and $k$ ($2 \leq n \leq 2\cdot 10^5$, $2 \leq m \leq 2\cdot 10^5$, $n \cdot m \leq 6\cdot 10^5$, $1 \leq k \leq n\cdot m$) — the number of rows and columns of the matrix $A$, and the range of the integers in the matrix, respectively.

翻译中…

Then $n$ lines follow, the $i$-th line containing $m$ integers $A_{i,1},A_{i,2},\ldots,A_{i,m}$ ($1 \leq A_{i,j} \leq k$ or $A_{i,j} = -1$) — the elements in $A$.

翻译中…

It is guaranteed that the sum of $n\cdot m$ over all test cases does not exceed $6\cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output a single integer — the maximum possible beauty.

翻译中…

Note

翻译中…

In the first test case, the matrix $A$ is already determined. Its beauty is

翻译中…

$$\sum_{u=1}^k \sum_{i=1}^{n-1} c_{u,i} \cdot c_{u,i+1} = c_{1,1}\cdot c_{1,2} + c_{1,2}\cdot c_{1,3} + c_{2,1}\cdot c_{2,2} + c_{2,2}\cdot c_{2,3} + c_{3,1}\cdot c_{3,2} + c_{3,2}\cdot c_{3,3} = 1\cdot 1 + 1\cdot 1 + 2\cdot 0 + 0\cdot 1 + 0\cdot 2 + 2\cdot 1 = 4.$$

翻译中…

In the second test case, one can fill the matrix as follows:

翻译中…

$$ \begin{bmatrix} 2 &3 &3 \\ 2 &2 &3 \end{bmatrix}, $$

翻译中…

and get the value $4$. It can be proven this is the maximum possible answer one can get.

翻译中…

In the third test case, one of the possible optimal configurations is:

翻译中…

$$ \begin{bmatrix} 1 &1 &1 \\ 1 &2 &1 \\ 1 &1 &4 \end{bmatrix}. $$

翻译中…

In the fourth test case, one of the possible optimal configurations is:

翻译中…

$$ \begin{bmatrix} 1 &3 &2 &3 \\ 1 &3 &2 &1 \\ 3 &1 &5 &1 \end{bmatrix}. $$

翻译中…

In the fifth test case, one of the possible optimal configurations is:

翻译中…

$$ \begin{bmatrix} 5 &5 &2 \\ 1 &8 &5 \\ 7 &5 &6 \\ 7 &7 &4 \\ 4 &4 &4 \end{bmatrix}. $$

翻译中…

样例

Input #1
9
3 3 3
1 2 2
3 1 3
3 2 1
2 3 3
-1 3 3
2 2 -1
3 3 6
-1 -1 1
1 2 -1
-1 -1 4
3 4 5
1 3 2 3
-1 -1 2 -1
3 1 5 1
5 3 8
5 -1 2
1 8 -1
-1 5 6
7 7 -1
4 4 4
6 6 5
-1 -1 5 -1 -1 -1
-1 -1 -1 -1 2 -1
-1 1 3 3 -1 -1
-1 1 -1 -1 -1 4
4 2 -1 -1 -1 4
-1 -1 1 2 -1 -1
6 6 4
-1 -1 -1 -1 1 -1
3 -1 2 2 4 -1
3 1 2 2 -1 -1
3 3 3 3 -1 2
-1 3 3 -1 1 3
3 -1 2 2 3 -1
5 5 3
1 1 3 -1 1
2 2 -1 -1 3
-1 -1 -1 2 -1
3 -1 -1 -1 2
-1 1 2 3 -1
6 2 7
-1 7
-1 6
7 -1
-1 -1
-1 -1
2 2
Output #1
4
4
10
10
8
102
93
58
13
InputCopy OutputCopy
题目信息
题号
2053F
时间限制
5 s
内存限制
512 MB
标签
暴力
数据结构
动态规划
贪心
模拟
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。