2019牛客暑期多校训练营(第八场)A All-one Matrices(单调栈)

链接: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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值