2019牛客暑期多校訓練營(第八場)All-one Matrices (單調棧)

鏈接:https://ac.nowcoder.com/acm/contest/888/A
來源:牛客網
 

時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 524288K,其他語言1048576K
64bit IO Format: %lld

題目描述

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)​.

題目大意:給出了一個01矩陣,讓求有多少個全一矩陣不被其他全1矩陣覆蓋。

解題思路:預處理出每一個點向上連續的1是多少即h[i][j],即確定上邊界。

我們依次枚舉下邊界第i行,對於當前點i,j h[i][j] 爲上邊界,然後用單調棧處理出 以上邊界爲最小值的左右邊界L,R。

然後只需要判斷一下,當前的下邊界能不能向下延伸就可以了。注意對於第i行同一個子矩陣可能被多次計算(高度相同),

所以我們維護一個高度遞減的單調棧就可以了。

#include<bits/stdc++.h>
using namespace std;
const int N = 3005;
typedef long long ll;
const int mmax = 1e5+5;
char s[N][N];
int sum[N][N],L[N],R[N];
int h[N][N];

stack<int>st;
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++)scanf("%s",s[i]+1);

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(s[i][j]-'0')h[i][j] = h[i-1][j]+1;
            sum[i][j] = sum[i][j-1] + s[i][j] - '0';
        }
    }
    int ans=0;

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            while(!st.empty() && h[i][j] <= h[i][st.top()]) st.pop();
            if(st.empty()) L[j] = 1;
            else L[j] = st.top()+1;
            st.push(j);
        }
        while(!st.empty())st.pop();
        
        for(int j=m; j>=1; j--)
        {
            while(!st.empty() && h[i][j] <= h[i][st.top()]) st.pop();
            if(st.empty()) R[j] = m;
            else R[j] = st.top()-1;
            st.push(j);
        }
        while(!st.empty())st.pop();

        for(int j=m; j>=1; j--)
        {
            if(h[i][j]==0)
            {
                while(!st.empty())st.pop();
                continue;
            }
            while(!st.empty() && h[i][j] < h[i][st.top()]) st.pop();

            if(st.empty() || h[i][j] != h[i][st.top()]) //避免高度相同的多次計算。
            {
                int l = L[j];
                int r = R[j];
                if(sum[i+1][r]-sum[i+1][l-1] != r - l + 1)ans++;
            }
            st.push(j);
        }
        while(!st.empty())st.pop();
    }
    cout<<ans<<endl;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章