一 原题
A Rectangular Barn
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: rectbarn
INPUT 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 2 1 3 2 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.
二 分析
三 代码
USER: Qi Shen [maxkibb3] TASK: rectbarn LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 13052 KB] Test 2: TEST OK [0.000 secs, 13052 KB] Test 3: TEST OK [0.000 secs, 13052 KB] Test 4: TEST OK [0.000 secs, 13052 KB] Test 5: TEST OK [0.000 secs, 13052 KB] Test 6: TEST OK [0.000 secs, 13052 KB] Test 7: TEST OK [0.014 secs, 13052 KB] Test 8: TEST OK [0.084 secs, 13052 KB] Test 9: TEST OK [0.112 secs, 13052 KB] Test 10: TEST OK [0.112 secs, 13052 KB] All tests OK.
Your program ('rectbarn') produced all correct answers! This is your submission #5 for this problem. Congratulations!
/*
ID:maxkibb3
LANG:C++
PROB:rectbarn
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
const int MAX = 3005;
int R, C, P, Ans;
bool Map[MAX][MAX];
int Up[MAX], Left[MAX], Right[MAX],
TLeft[MAX], TRight[MAX];
int main() {
freopen("rectbarn.in", "r", stdin);
freopen("rectbarn.out", "w", stdout);
scanf("%d%d%d", &R, &C, &P);
int x, y;
while(P--) {
scanf("%d%d", &x, &y);
Map[x][y] = true;
}
for(int i = 1; i <= C; i++)
Left[i] = Right[i] = MAX;
for(int i = 1; i <= R; i++) {
for(int j = C; j >= 1; j--) {
if(Map[i][j])
TRight[j] = 0;
else
TRight[j] = TRight[j + 1] + 1;
}
for(int j = 1; j <= C; j++) {
if(Map[i][j]) {
TLeft[j] = 0;
Up[j] = 0;
Left[j] = Right[j] = MAX;
}
else {
TLeft[j] = TLeft[j - 1] + 1;
Up[j]++;
Left[j] = std::min(Left[j], TLeft[j]);
Right[j] = std::min(Right[j], TRight[j]);
}
int tmp = (Left[j] + Right[j] - 1) * Up[j];
Ans = std::max(Ans, tmp);
}
}
printf("%d\n", Ans);
return 0;
}