#P6070. 『MdOI R1』Decrease
『MdOI R1』Decrease
Problem Description
Given an matrix, you may perform several operations.
In each operation, you can add to all numbers in a contiguous submatrix, or subtract from all numbers in a contiguous submatrix.
Initially, there are positions in the matrix whose values are non-zero, and all other positions are .
Please find the minimum number of operations required to make all numbers in the matrix become .
Input Format
The first line contains three integers , representing the matrix size, the number of non-zero cells, and the size of the contiguous submatrix modified in each operation.
The next lines each contain three integers , meaning that initially the value at row , column of the matrix is .
Output Format
Output one integer in a single line, representing the minimum number of operations.
In particular, if it is impossible to make all numbers in the matrix become , output -1.
4 14 3
1 1 1
1 2 1
1 3 1
2 1 1
2 2 3
2 3 3
2 4 2
3 1 1
3 2 3
3 3 3
3 4 2
4 2 2
4 3 2
4 4 2
3
3 1 2
1 1 1
-1
4 5 1
1 1 5
2 2 -3
2 3 -4
3 3 1
4 4 2
15
Hint
[Sample 1 Explanation]:
The given matrix is:
1 1 1 0
1 3 3 2
1 3 3 2
0 2 2 2
Steps:
First, perform the subtract 1 operation once on the contiguous submatrix whose top-left corner is at row 1, column 1.
Then, perform the subtract 1 operation twice on the contiguous submatrix whose top-left corner is at row 2, column 2.
In total, 3 operations.
1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0
1 3 3 2 0 2 2 2 0 1 1 1 0 0 0 0
1 3 3 2 0 2 2 2 0 1 1 1 0 0 0 0
0 2 2 2 0 2 2 2 0 1 1 1 0 0 0 0
[Sample 2 Explanation]:
The given matrix is:
1 0 0
0 0 0
0 0 0
It is impossible to make all cells become using only operations on contiguous submatrices.
[Constraints]
This problem uses bundled testdata.
| Subtask ID | Score | ||
|---|---|---|---|
| 1 | 11 | ||
| 2 | 14 | ||
| 3 | 17 | ||
| 4 | 34 | ||
| 5 | 24 | ||
For all testdata, , , , , each pair appears at most once, and .
The testdata guarantees that if a solution exists, the answer does not exceed .
[Hint]
The input size is large, so it is recommended to use a faster input method.
Translated by ChatGPT 5