列表

详情


NC51545. All-one Matrices

描述

Gromah and LZR entered the great tomb, the first thing they see is a matrix of size , and the elements in the matrix are all  or .

LZR finds a note board saying "An all-one matrix is defined as the matrix whose elements are all , 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 , denoting the size of given matrix.

Following lines each contains a string with length , whose elements are all or , denoting the given matrix.


输出描述

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)_{}.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 151ms, 内存消耗: 39640K, 提交时间: 2019-08-11 13:44:27

#include<bits/stdc++.h>
#define maxn 3005
#define LL long long
using namespace std;
int n, m, a[maxn][maxn], ans, s[maxn], pre[maxn], q[maxn], tp;
char c[maxn];
int main()
{
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) {
		scanf("%s", c + 1);
		for (int j = 1; j <= m; j++) a[i][j] = c[j] - '0';
	}
	for (int i = 1; i <= n; i++) {
		tp = 0;
		for (int j = 1; j <= m + 1; j++) {
			s[j] = (a[i][j] ? s[j] + 1 : 0), pre[j] = pre[j - 1] + a[i + 1][j];
			while (s[q[tp]] > s[j]) ans += (pre[j - 1] - pre[q[tp - 1]] < j - 1 - q[tp - 1] && s[q[tp - 1]] < s[q[tp]]), tp--;
			q[++tp] = j;
		}
	}
	printf("%d", ans);
}

C++11(clang++ 3.9) 解法, 执行用时: 129ms, 内存消耗: 39648K, 提交时间: 2019-08-10 22:44:59

#include<bits/stdc++.h>
#define maxn 3005
#define LL long long
using namespace std;
int n,m,a[maxn][maxn],ans,s[maxn],pre[maxn],q[maxn],tp;
char c[maxn];
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%s",c+1);
        for(int j=1;j<=m;j++) a[i][j]=c[j]-'0';
    }
    for(int i=1;i<=n;i++){
        tp=0;
        for(int j=1;j<=m+1;j++){
            s[j]=(a[i][j]?s[j]+1:0),pre[j]=pre[j-1]+a[i+1][j];
            while(s[q[tp]]>s[j]) ans+=(pre[j-1]-pre[q[tp-1]]<j-1-q[tp-1]&&s[q[tp-1]]<s[q[tp]]),tp--;
            q[++tp]=j;
        }
    }
    printf("%d",ans);
}

上一题