bzoj3039 玉蟾宫【单调栈】
2017-12-18 16:15
232 查看
解题思路:
枚举每一行作为矩形的底,h[j]表示第j列向上延伸的最多的1,再用两个单调栈维护每列向左和向右第一个h值比它小的列l[j],r[j],那么矩形面积即是h[j]*(r[j]-l[j]-1)。可以证明最优解一定会被统计。时间复杂度为O(n2)
#include<bits/stdc++.h> #define ll long long < 4000 span class="hljs-keyword">using namespace std; int getint() { int i=0,f=1;char c; for(c=getchar();(c<'0'||c>'9')&&c!='-';c=getchar()); if(c=='-')f=-1,c=getchar(); for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0'; return i*f; } const int N=1005; int n,m,ans; int a ,h ,l ,r ,sta ; char s[2]; int main() { //freopen("lx.in","r",stdin); n=getint(),m=getint(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { scanf("%s",s); a[i][j]=s[0]=='F'?1:0; } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) h[j]=a[i][j]?h[j]+1:0; sta[0]=0; for(int j=1,top=0;j<=m;j++) { while(top&&h[j]<=h[sta[top]])top--; l[j]=sta[top]+1,sta[++top]=j; } sta[0]=m+1; for(int j=m,top=0;j;j--) { while(top&&h[j]<=h[sta[top]])top--; r[j]=sta[top]-1,sta[++top]=j; } for(int j=1;j<=m;j++) ans=max(ans,h[j]*(r[j]-l[j]+1)); } cout<<ans*3<<'\n'; return 0; }
相关文章推荐
- 【BZOJ】3039: 玉蟾宫(DP/单调栈)
- CODEVS 2491 玉蟾宫 (单调栈或者dp乱搞什么的)
- 【数据结构】CODE[VS] 2491 && bzoj 3039玉蟾宫 (单调栈)
- bzoj3039: 玉蟾宫
- BZOJ3039-玉蟾宫
- BZOJ3039: 玉蟾宫&wikioi2491 玉蟾宫
- [Tyvj1939] 玉蟾宫(单调栈)
- [BZOJ 3039&洛谷P4147]玉蟾宫 题解(单调栈)
- 51nod-1158 . 全是1的最大子矩阵 &&CODEVS-2491 玉蟾宫(枚举+单调栈 or dp)
- bzoj3039: 玉蟾宫
- 【bzoj3039】玉蟾宫 悬线法(单调栈)
- bzoj 3039: 玉蟾宫 单调栈或者悬线法求最大子矩阵和
- POJ 2082 Terrible Sets 单调栈的应用
- POJ3250 牛的视野(单调栈)
- Poetize4 玉蟾宫
- poj3250 Bad Hair Day(单调栈)
- poj 2726 Holiday Hotel (排序&&单调栈)
- HDU 5033 Building 单调栈 2014网络赛北京站B题
- hdu - 2870 - Largest Submatrix(dp / 单调栈)
- [BZOJ 3238] [AHOI 2013] 差异 【后缀数组 + 单调栈】