链接: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.
示例1
输入
复制
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).
单调栈枚举每个右边最大,左边最大,上面最大,且下面不会重复的情况
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3005;
char mp[maxn][maxn];
int num[maxn][maxn];
stack< pair<int, int> >q;
int main() {
int n, m;
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> mp[i][j];
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
num[i][j] = (mp[i][j] == '1') ? num[i - 1][j] + 1 : 0;
int ans = 0;
for (int i = 1; i <= n; i++) {
int tmp = -1;
while (!q.empty())q.pop();
for (int j = 1; j <= m; j++) {
int pos = j;
while (!q.empty() && q.top().first > num[i][j]) {
if (q.top().second <= tmp)ans++;
pos = q.top().second;
q.pop();
}
if (!num[i + 1][j])tmp = j;
if (num[i][j] && (q.empty() || q.top().first < num[i][j]))
q.push({ num[i][j],pos });
}
}
cout << ans << "\n";
return 0;
}