链接:https://ac.nowcoder.com/acm/contest/888/A
题目描述
Gromah and LZR entered the great tomb, the first thing they see is a matrix of size n×mn\times mn×m, and the elements in the matrix are all 00_{}0 or 11_{}1.
LZR finds a note board saying “An all-one matrix is defined as the matrix whose elements are all 11_{}1, you should determine the number of all-one submatrices of the given matrix that are not completely included by any other all-one submatrices”.
Meanwhile, Gromah also finds a password lock, obviously the password should be the number mentioned in the note board!
Please help them determine the password and enter the next level.
输入描述:
The first line contains two positive integers n,mn,m_{}n,m, denoting the size of given matrix.
Following nn_{}n lines each contains a string with length mm_{}m, whose elements are all 00_{}0 or 11_{}1, denoting the given matrix.
1≤n,m≤30001\le n,m \le 30001≤n,m≤3000
输出描述:
Print a non-negative integer, denoting the answer.
输入
3 4
0111
1110
0101
输出
5
说明
The 5 matrices are (1,2)−(1,4), (1,2)−(2,3), (1,2)−(3,2), (2,1)−(2,3), (3,4)−(3,4)(1,2)-(1,4), ; (1,2)-(2,3), ; (1,2)-(3,2), ; (2,1)-(2,3), ; (3,4)-(3,4)_{}(1,2)−(1,4),(1,2)−(2,3),(1,2)−(3,2),(2,1)−(2,3),(3,4)−(3,4).
赛后补题,赛场上一点思路没有。。
poj上有一个最大全一矩阵的题目,也是单调栈的题目。但是没想起来怎么用单调栈解决这个问题。对于每一个num[i][j]我们记录它向上的连续一的个数,然后枚举每一行,从前向后枚举列。单调找维护两个东西,就是向上的最长长度和矩阵的最左位置pos。那么我们把最上最左可到达的距离记录下来了,不能向右扩,那么只看能不能向下扩就行了。如果不能的话,就到头了。
代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=3e3+100;
int a[maxx][maxx];
int num[maxx][maxx];
stack< pair<int,int> > s;
int n,m;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
scanf("%1d",&a[i][j]);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
num[i][j]=a[i][j]?num[i-1][j]+a[i][j]:0;//记录每一列的连续1的前缀和
}
int ans=0;
for(int i=1;i<=n;i++)
{
int tmp=-1;
while(!s.empty()) s.pop();
for(int j=1;j<=m+1;j++)
{
int pos=j;
while(!s.empty()&&s.top().first>num[i][j])
{
if(s.top().second<=tmp) ans++;//这个矩阵最左边小于当前不可扩的最右边,就说明这个矩阵不可扩
pos=s.top().second;//更新pos就是下个矩阵的最左边。
s.pop();
}
if(!num[i+1][j]) tmp=j;//下面是0就不可下扩,那样就把最右边的记录下来
if(num[i][j]&&(s.empty()||s.top().first<num[i][j])) s.push(make_pair(num[i][j],pos));//入栈的是矩阵的最左边(i+num-1,j)->(i,j)就是一个矩阵
}
}
printf("%d\n",ans);
}
努力加油a啊,(o)/~