One is Good, but Two is Better
Time limit1sMemory limit128 MB
Given an N by M grid of 0, 1, and 2, cover every 2 with two rectangles that avoid 1, minimizing the total area covered.
- Level
Medium7 of 10
- Topics
- Brute force, Prefix sum, Matrix, Greedy
- Solved
- No attempts yet
Problem
You are given an matrix whose entries are each , , or . At least one entry equals .
Choose two axis-aligned rectangles (they may overlap, and they may even be identical) so that:
- every cell equal to lies inside at least one of the two rectangles, and
- neither rectangle contains any cell equal to (cells equal to are allowed inside a rectangle).
The area of a rectangle is the number of cells it covers. Among all valid choices, minimize the area of the region covered by the two rectangles together (a cell covered by both rectangles is counted once).
Report that minimum combined area, or report that no valid pair of rectangles exists.
Input
The first line contains two integers and . Each of the next lines contains integers, giving the matrix row by row; every value is , , or .
Output
Print a single integer: the minimum combined area of the two rectangles, or if no valid pair exists.
Constraints
- Every matrix entry is , , or .
- At least one entry equals .