USACO 6.1.2 rectbarn
题目:
http://ace.delos.com/usacoprob2?a=JsPN70PkuJW&S=rectbarn
http://pingce.ayyz.cn:9000/usaco/data/20110129214306/rectbarn.html
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.
题解:
这题与03年王知昆的论文中所举的例子并无二异,论文当中给出了两种做法,一种是O(P^2)的,另一种是O(R*C)。本题由于数据范围的约束只能使用第二种悬线的NB的dp,详见论文。
1 /*
2 ID:zhongha1
3 PROB:rectbarn
4 LANG:C++
5 */
6
7 #include<cstdio>
8 #include<cstdlib>
9 #include<cstring>
10 #include<algorithm>
11
12 using namespace std;
13
14 const int maxn=3001;
15
16 int n,m,p,ans,left[maxn],right[maxn],h[2][maxn],l[2][maxn],r[2][maxn];
17
18 bool use[maxn][maxn];
19
20 int main()
21 {
22 freopen("rectbarn.in","r",stdin);
23 freopen("rectbarn.out","w",stdout);
24
25 scanf("%d%d%d",&n,&m,&p);
26 for (int a=1;a<=p;a++)
27 {
28 int x,y;
29 scanf("%d%d",&x,&y);
30 use[x][y]=true;
31 }
32 for (int a=1;a<=m;a++)
33 l[0][a]=1,r[0][a]=m;
34 for (int a=1;a<=n;a++)
35 {
36 int nowl=1;
37 for (int b=1;b<=m;b++)
38 if (use[a][b]) nowl=b+1;
39 else left[b]=nowl;
40 nowl=m;
41 for (int b=m;b>=1;b--)
42 if (use[a][b]) nowl=b-1;
43 else right[b]=nowl;
44 for (int b=1;b<=m;b++)
45 if (use[a][b])
46 {
47 h[a % 2][b]=0;
48 l[a % 2][b]=0;
49 r[a % 2][b]=m;
50 }
51 else
52 {
53 h[a % 2][b]=h[1-a % 2][b]+1;
54 l[a % 2][b]=max(left[b],l[1-a % 2][b]);
55 r[a % 2][b]=min(right[b],r[1-a % 2][b]);
56 ans=max(h[a % 2][b]*(r[a % 2][b]-l[a % 2][b]+1),ans);
57 }
58 }
59 printf("%d\n",ans);
60
61 return 0;
62 }