#P6469. [COCI 2008/2009 #6] NERED
[COCI 2008/2009 #6] NERED
Description
There is an grid. Each row and each column has cells. Each cell contains a number, and initially all numbers are . There are modifications, and each modification adds to the number in a specific cell.
Define one operation as decreasing the number in a non-zero cell by , and increasing the number in another cell (which may be ) by .
Find the minimum number of operations needed so that every cell contains either or , and all cells with value form a rectangle. Output the number of operations.
Input Format
The first line contains two integers, representing the grid size and the number of modifications .
The next lines each contain two integers , meaning the number in the cell at row , column is increased by .
Output Format
Output one line with one integer, the answer.
3 2
1 1
1 1
1
4 3
2 2
4 4
1 1
2
5 8
2 2
3 2
4 2
2 4
3 4
4 4
2 3
2 3
3
Hint
Sample Explanation
Explanation for Sample 1
Subtract from row , column , and add to row , column .
Explanation for Sample 3
- Subtract from row , column , and add to row , column .
- Subtract from row , column , and add to row , column .
- Subtract from row , column , and add to row , column .
Constraints
For all test points, it is guaranteed that:
- , .
- .
- A solution always exists for the input.
Notes
This problem is translated from COCI2008-2009 CONTEST #6 T3 NERED. Translation by @一扶苏一.
Translated by ChatGPT 5
京公网安备 11011102002149号