A rectangular barn
Mircea pasoi -- 2003
Ever the capitalist, Farmer John wants to extend his milking business by purchasing more cows. He needs space to build a new barn for the cows.
FJ purchased a rectangular field with r (1 ≤ r ≤ 3,000) rows numbered 1 .. R and C (1 ≤ C ≤ 3,000) columns numbered 1 .. c. unfortunately, he realized too late that some 1x1 areas in the field are damaged, so he cannot
Build the barn on the entire RXC field.
FJ has counted P (0 ≤ p ≤30,000) damaged 1x1 pieces and has asked for your help to find the biggest rectangular barn (I. E ., the largest area) that he can build on his land without building on the damaged pieces.
Program name: rectbarninput format
- Line 1: three space-separated integers: R, C, and P.
- Lines 2. p + 1: Each line contains two space-separated integers, R and C, that give the row and column numbers of a damaged area of the field
Sample input (File rectbarn. In)
3 4 21 32 1
Output Format
- Line 1: the largest possible area of the new Barn
Sample output (File rectbarn. out)
6
Output details
1 2 3 4 +-+ 1 | x | +-+ 2 | x |##|#| + -+ 3 | # | +-+
Pieces marked with 'X' are damaged and pieces marked with '# 'are part of the new Barn.
Question: give you a matrix of N * M. Some squares cannot be used to obtain the largest submatrix area.
Analysis: There are two solutions to the maximum submatrix problem: click on a point, discretization and scanning, complexity O (P ^ 2), and lattice-by-grid scan and DP... The complexity is O (RC), which is analyzed by data volume. This is suitable for the second method... For more information, see Wang zhikun's thesis.
PS: the data for this question is too big?
Code:
/* ID: 15114582 prog: rectbarnlang: C ++ */# include <cstdio> # include <iostream> using namespace STD; const int Mm = 3003; int L [mm], H [mm], R [mm]; bool G [mm] [mm]; int I, J, K, lm, RM, n, m, ans; int main () {freopen ("rectbarn. in "," r ", stdin); freopen (" rectbarn. out "," W ", stdout); While (~ Scanf ("% d", & N, & M, & K) {for (I = 0; I <= N; ++ I) for (j = 0; j <= m; ++ J) g [I] [J] = 1; while (k --) {scanf ("% d ", & I, & J); G [I] [J] = 0 ;}for (ANS = J = 0; j <= m; ++ J) H [J] = 0, L [J] = 1, R [J] = m; for (I = 1; I <= N; ++ I) {for (LM = j = 1; j <= m; ++ J) if (G [I] [J]) ++ H [J], L [J] = max (L [J], lm); else h [J] = 0, L [J] = 1, R [J] = m, lm = J + 1; for (Rm = J = m; j> = 1; -- j) if (G [I] [J]) {R [J] = min (R [J], RM); ans = max (ANS, (R [J]-l [J] + 1) * H [J]);} else Rm = J-1;} printf ("% d \ n", ANS);} return 0 ;}