https://www.luogu.org/problemnew/show/P4147
滚动数组+悬线法
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#define ll long long
#define pb push_back
#define test printf("here!!!")
using namespace std;
const int mx=2000+10;
int mp[mx][mx];
int h[mx];
int l[mx];
int r[mx];
int lo,ro;
int maxn=0;
int getc()
{
char c=getchar();
while (c!='F'&&c!='R') c=getchar();
return c=='F'?1:0;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for (int i=1;i<=n;++i)
{
for (int j=1;j<=m;++j)
{
mp[i][j]=getc();
}
}
for (int i=1;i<=m;++i) l[i]=1,r[i]=m;
for (int i=1;i<=n;++i)
{
lo=0;
for (int j=1;j<=m;++j)
{
if (mp[i][j]^1)
{
lo=j;
h[j]=l[j]=0;
}
else
{
h[j]++;
l[j]=max(lo+1,l[j]);
}
}
ro=m+1;
for (int j=m;j>=1;--j)
{
if (mp[i][j]^1)
{
ro=j;
r[j]=m+1;
}
else
{
r[j]=min(ro-1,r[j]);
maxn=max(maxn,h[j]*(r[j]-l[j]+1));
}
}
//printf("%d %d %d \n",h[5],l[5],r[5]);
}
printf("%d\n",3*maxn);
}
单调栈+二维
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#define ll long long
#define pb push_back
#define test printf("here!!!")
using namespace std;
const int mx=2000+10;
int mp[mx][mx];
int h[mx];//记录行高度,同悬线法的up/h的滚动数组
struct Node
{
int h;//记录高度
int pos;//记录下标
}ddz[mx];//模拟单调栈
int maxn=0;
int getc()
{
char c=getchar();
while (c!='F'&&c!='R') c=getchar();
return c=='F'?1:0;
}
int cnt;
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for (int i=1;i<=n;++i)
{
for (int j=1;j<=m;++j)
{
mp[i][j]=getc();
}
}
for (int i=1;i<=n;++i)
{
cnt=0;
h[m+1]=-1;//最后插入一个高度为-1的来清空栈,保证最后一个也算过
for (int j=1;j<=m+1;++j)
{
if (j!=m+1)
{
if (mp[i][j]==1) h[j]++;
else h[j]=0;
}//每行滚动高度
if (!cnt) ddz[++cnt].pos=j,ddz[cnt].h=h[j];//空栈直接插入
else if (h[j]>ddz[cnt].h)//大于栈顶直接插入
{
ddz[++cnt].pos=j;
ddz[cnt].h=h[j];
}
else
{
int tp=j;
while (cnt&&h[j]<=ddz[cnt].h)//小于等于栈顶,持续弹出
{
maxn=max(maxn,ddz[cnt].h*(j-ddz[cnt].pos));//宽度为(j-ddz[cnt].pos),意味着pos的高度可以向右延伸到j-1这里,注意在这里[pos,j-1]都是单调递增的
tp=ddz[cnt].pos;//注意这里,意味着新插进来的这块预计可以扩展到.pos这里,因为[pos,j-1]的高度都比h[j]高,所以以h[j]的高度向左可以延伸到pos这里
cnt--;
}
ddz[++cnt].pos=tp;//插入
ddz[cnt].h=h[j];
}
}
}
printf("%d\n",3*maxn);
}
单调栈+二维 参考博客:
https://blog.youkuaiyun.com/hopeztm/article/details/7868581
https://blog.youkuaiyun.com/sweeterer/article/details/52053962