转载:https://blog.youkuaiyun.com/hopeztm/article/details/7868581
poj 3494 Largest Submatrix of All 1’s
#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include <cstdio>
#include <vector>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=2005;
int n,m;
int a[maxn][maxn],b[maxn][maxn];
char mapp[maxn][maxn];
struct node
{
int height;
int xb;
node(int a,int b)
{
height=a;xb=b;
}
};
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
b[i][j]=a[i][j]==0?0:b[i-1][j]+1;
}
}
int maxi=0;
stack<node>st;
for(int i=1;i<=n;i++)
{
st.push(node(0,0));
for(int j=1;j<=m;j++)
{
int nh=b[i][j];
node tp(nh,j);
while(st.top().height>nh)
{
tp=st.top();
st.pop();
maxi=max(maxi,(j-tp.xb)*tp.height);
}
st.push(node(nh,tp.xb));
}
while(!st.empty())
{
node tp=st.top();
st.pop();
maxi=max(maxi,(m+1-tp.xb)*tp.height);
}
}
printf("%d\n",maxi);
}
return 0;
}
牛客 638 幸运的wnm
#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=1005;
int n,m;
int a[maxn][maxn],b[maxn][maxn];
char mapp[maxn][maxn];
struct node
{
int height;
int xb;
node(int a,int b)
{
height=a;xb=b;
}
};
int main()
{
ios::sync_with_stdio(false);
int T;
cin>>T;
while(T--)
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
cin>>mapp[i][j];
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
a[i][j]=mapp[i][j]-'0';
}
}
memset(b,0,sizeof(b));
for(int u=1;u<=n;u++)
{
for(int i=1;i<=m;i++)
{
if(u!=1&&b[u-1][i]>=u)
{
b[u][i]=b[u-1][i]-1;
continue;
}
int cnt=0;
for(int j=u;j<=n;j++)
{
if(a[j][i]==1)
{
cnt++;
}
else
break;
}
b[u][i]=cnt;
}
}
int maxi=0;
stack<node>st;
for(int i=1;i<=n;i++)
{
st.push(node(0,0));
for(int j=1;j<=m;j++)
{
int nh=b[i][j];
node tp(nh,j);
while(st.top().height>nh)
{
tp=st.top();
st.pop();
maxi=max(maxi,(j-tp.xb)*tp.height);
}
st.push(node(nh,tp.xb));
}
while(!st.empty())
{
node tp=st.top();
st.pop();
maxi=max(maxi,(m+1-tp.xb)*tp.height);
}
}
cout << maxi << endl;
}
return 0;
}