Largest Square
Time limit2sMemory limit128 MB
Given an N by N grid with W bad cells listed explicitly, find the largest axis-aligned square containing at most L bad cells.
- Level
Medium7 of 10
- Topics
- Binary search, Prefix sum, Two pointers, Brute force
- Solved
- No attempts yet
Problem
There is an mosaic of square solar cells (). Each solar cell is either good or bad. There are bad cells (). You must find the largest square within the mosaic that contains at most () bad cells.
Input
The first line of input contains a single integer (), the number of test cases. The test cases then follow.
The first line of a test case contains three space-separated integers , , and . The next lines each contain two space-separated integers giving the row and column (each from to ) of a bad solar cell.
Output
For each input instance, output a single integer: the area of the largest square that contains no more than bad solar cells.
Hint
Suppose the mosaic is with the following arrangement of good ('G') and bad ('B') cells:
BGGG
GBBG
GGGG
GGGG
Several squares near the bottom contain no bad cells, but every square contains at least two bad cells.