City Game
HDU - 1505题意:开发商在一块m*n的地皮上建楼房, 一直这块地有空地和已用地, 空地用F表示, 已用地用R表示, 每单位面积的地皮市值3$, 求出这整块地皮上价值最大的一块矩形地皮;
HDU 1506 | Largest Rectangle in a Histogram |
这道题与1506的区别在于它变成二维的了;
那么我们一层一层的求不就好了;
h[i][j]表示第i层第j列的高度, l[i][j]表示 第i层第j列左边第一个小于它的数, r[i][j]表示 第i层第j列右边第一个小于它的数;
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;
int G[1100][1100], h[1100][1100], l[1100][1100], r[1100][1100];
stack<int> sta;
int main(){
int T;
scanf("%d", &T);
while(T--){
int m, n;
scanf("%d%d", &m, &n);
char ch[5];
for(int i=1; i<=m; i++){
for(int j=1; j<=n; j++){
scanf("%s", ch);
if(ch[0]=='R') G[i][j]=1;
else G[i][j]=0;
}
}
memset(h, 0, sizeof(h));
for(int i=1; i<=m; i++){
for(int j=1; j<=n; j++){
if(G[i][j]==0) h[i][j]=h[i-1][j]+1;
else h[i][j]=0;
}
}
memset(l, 0, sizeof(l));
memset(r, 0, sizeof(r));
for(int i=1; i<=m; i++){
while(!sta.empty()) sta.pop();
for(int j=1; j<=n; j++){
while(!sta.empty()&&h[i][j]<=h[i][sta.top()]) sta.pop();
if(sta.empty()) l[i][j]=0;
else l[i][j]=sta.top();
sta.push(j);
}
while(!sta.empty()) sta.pop();
for(int j=n; j>0; j--){
while(!sta.empty()&&h[i][j]<=h[i][sta.top()]) sta.pop();
if(sta.empty()) r[i][j]=n+1;
else r[i][j]=sta.top();
sta.push(j);
}
}
int ans=0;
for(int i=1; i<=m; i++){
for(int j=1; j<=n; j++){
ans=max(ans, h[i][j]*(r[i][j]-l[i][j]-1));
}
}
printf("%d\n", ans*3);
}
return 0;
}