Usaco 6.1 A rectangular barn (maximum submatrix)

Source: Internet
Author: User

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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.