usaco6.1.2 A Rectangular Barn

这篇博客讲述了农场主约翰计划扩大奶牛业务,需要购买更多奶牛并建造新牛棚。他买下了一块矩形土地,但由于部分区域损坏无法使用。约翰需要找出能建造的最大矩形牛棚面积,即不包括损坏区域的最大矩形区域。博客包括问题描述、输入输出格式、样例及代码分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 原题

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: 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. 


二 分析

和5.3.4 Big Barn非常类似。只不过5.3.4要求找出所有无障碍的正方形,现在条件放松了,找出所有无障碍的长方形。
初始的想法还是动规求出以点(i,j)为右下角的面积最大的长方形,但这样的状态表示并不满足最优子结构。
正确思路是:答案里面积最大的长方形一定是无法再向上、左、右扩张的。我们可以定义Up[i][j]表示点(i,j)最多能向上延伸多少格,用Left[i][j]和Right[i][j]表示这条延伸的线段最多能向左、右平移多少格。这样可以找到一个“极大长方形”。遍历所有点,一定可以得到合法的面积最大的长方形。


三 代码

运行结果:
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!


AC代码:
/*
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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值