bzoj2228 礼物 单调队列

       首先枚举三个面作为底面。然后对于每一层,求出f[i][j]表示以i,j为右下角坐标的正方形的最大边长。然后枚举x,y表示以x,y为取出的长方体的正方形的底面,然后就变成了单调队列求区间长度*区间最小值的最大值的经典问题了。

AC代码如下(跑得好慢QAQ):

#include<iostream>
#include<cstdio>
#include<cstring>
#define N 151
using namespace std;

int n,m,w,a[N][N][N],b[N][N][N],f[N][N][N],c[N],h[N],l[N],r[N],ans;
char s[N];
void turn1(){
	int i,j,k;
	for (i=1; i<=m; i++)
		for (j=1; j<=n; j++)
			for (k=1; k<=w; k++) a[i][w-k+1][j]=b[i][j][k];
	swap(n,w);
}
void turn2(){
	int i,j,k; swap(n,w);
	for (i=1; i<=m; i++)
		for (j=1; j<=n; j++)
			for (k=1; k<=w; k++) a[k][j][m-i+1]=b[i][j][k];
	swap(m,w);

}
int calc(int cnt){
	int i,j=1,tmp=0; c[0]=c[cnt+1]=-1; h[1]=0;
	for (i=1; i<=cnt; i++){
		while (c[i]<=c[h[j]]) j--;
		l[i]=h[j]+1; h[++j]=i;
	}
	j=1; h[1]=cnt+1;
	for (i=cnt; i; i--){
		while (c[i]<=c[h[j]]) j--;
		r[i]=h[j]-1; h[++j]=i;
	}
	for (i=1; i<=cnt; i++) tmp=max(tmp,(r[i]-l[i]+1)*c[i]);
	return tmp;
}
void solve(){
	int i,j,k;
	for (k=1; k<=w; k++)
		for (i=1; i<=m; i++)
			for (j=1; j<=n; j++)
				if (a[i][j][k]) f[i][j][k]=min(min(f[i][j-1][k],f[i-1][j][k]),f[i-1][j-1][k])+1;
				else f[i][j][k]=0;
	for (i=1; i<=m; i++)
		for (j=1; j<=n; j++){
			memcpy(c,f[i][j],sizeof(f[i][j]));
			ans=max(ans,calc(w));
		}
}
int main(){
	scanf("%d%d%d",&m,&n,&w); int i,j,k;
	for (j=1; j<=n; j++)
		for (i=1; i<=m; i++){
			char ch=getchar(); while (ch<'A' || ch>'Z') ch=getchar();
			for (k=1; k<=w; k++){
				a[i][j][k]=b[i][j][k]=(ch=='N')?1:0; ch=getchar();
			}
		}
	solve(); turn1(); solve(); turn2();
	solve(); printf("%d\n",ans<<2);
	return 0;
}

by lych
2016.2.26

你可能感兴趣的:(单调队列)