A Strip of Land
Time limit2sMemory limit128 MB
Given a U by V grid of heights, find the largest-area rectangle whose height range is at most C and whose width is at most 100.
- Level
Hard8 of 10
- Topics
- Sliding window, Matrix, Two pointers, Brute force
- Solved
- No attempts yet
Problem
The residents of Dingilville want to choose a region of land on which to build an airport. They have a map of the terrain: a rectangular grid of unit squares. Each square is identified by a pair of coordinates , where is the horizontal (west–east) coordinate and is the vertical (south–north) coordinate. The map lists the height of every square.
Find a rectangular region of squares with the largest area (that is, the largest number of squares) such that
- the height difference between the highest and the lowest square of the region is at most a given limit , and
- the width of the region (the number of squares in the west–east direction) is at most .
Report the area of such a largest region.
Input
- The first line contains three integers , , and .
- Each of the next lines contains the heights for . More precisely, is the -th number on the -th input line, so the first data line holds the northernmost row () and the last data line holds the southernmost row ().
Output
Print a single integer: the largest possible area (number of squares) of a region that satisfies both conditions.
Constraints
- and , where is the number of squares in the west–east direction and the number in the south–north direction.
- .
- , where is the height of the square at coordinates , with and .
- The southwest corner square of the map has coordinates and the northeast corner has coordinates .