City Game HDU - 1505(单调栈)

City Game

HDU - 1505
题意:开发商在一块m*n的地皮上建楼房, 一直这块地有空地和已用地, 空地用F表示, 已用地用R表示, 每单位面积的地皮市值3$, 求出这整块地皮上价值最大的一块矩形地皮;
HDU 1506 Largest Rectangle in a Histogram
HDU1506的升级版;题解看这里: HDU 1506
这道题与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;
} 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值