您的位置:首页 > 其它

USACO 6.1.2 rectbarn

2012-07-01 19:10 239 查看
题目:

http://ace.delos.com/usacoprob2?a=JsPN70PkuJW&S=rectbarn

http://pingce.ayyz.cn:9000/usaco/data/20110129214306/rectbarn.html

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.

题解:

  这题与03年王知昆的论文中所举的例子并无二异,论文当中给出了两种做法,一种是O(P^2)的,另一种是O(R*C)。本题由于数据范围的约束只能使用第二种悬线的NB的dp,详见论文。

View Code

/*
ID:zhongha1
PROB:rectbarn
LANG:C++
*/

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>

using namespace std;

const int maxn=3001;

int n,m,p,ans,left[maxn],right[maxn],h[2][maxn],l[2][maxn],r[2][maxn];

bool use[maxn][maxn];

int main()
{
freopen("rectbarn.in","r",stdin);
freopen("rectbarn.out","w",stdout);

scanf("%d%d%d",&n,&m,&p);
for (int a=1;a<=p;a++)
{
int x,y;
scanf("%d%d",&x,&y);
use[x][y]=true;
}
for (int a=1;a<=m;a++)
l[0][a]=1,r[0][a]=m;
for (int a=1;a<=n;a++)
{
int nowl=1;
for (int b=1;b<=m;b++)
if (use[a][b]) nowl=b+1;
else left[b]=nowl;
nowl=m;
for (int b=m;b>=1;b--)
if (use[a][b]) nowl=b-1;
else right[b]=nowl;
for (int b=1;b<=m;b++)
if (use[a][b])
{
h[a % 2][b]=0;
l[a % 2][b]=0;
r[a % 2][b]=m;
}
else
{
h[a % 2][b]=h[1-a % 2][b]+1;
l[a % 2][b]=max(left[b],l[1-a % 2][b]);
r[a % 2][b]=min(right[b],r[1-a % 2][b]);
ans=max(h[a % 2][b]*(r[a % 2][b]-l[a % 2][b]+1),ans);
}
}
printf("%d\n",ans);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: