zoukankan      html  css  js  c++  java
  • 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 imes mn×m, and the elements in the matrix are all or 1.

    LZR finds a note board saying "An all-one matrix is defined as the matrix whose elements are all 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,m, denoting the size of given matrix.
     
    Following lines each contains a string with length m, whose elements are all or 1, denoting the given matrix.
     
     
    1≤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)

    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define maxn 3005
    int a[maxn][maxn];
    int h[maxn];//存该列往上有多少个1
    int l[maxn],r[maxn];
    int vis[maxn][maxn];
    struct node{
        int L,R;
    }hv[maxn][maxn];
    int main()
    {
        ios::sync_with_stdio(false);
        int n,m;
        int ans=0;
        cin>>n>>m;
        char ch;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cin>>ch;
                a[i][j]=ch=='0'?0:1;
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(a[i][j]==1) h[j]++;
                else h[j]=0;
            }
            h[0]=h[m+1]=-1;
            for(int j=1;j<=m;j++)//往左最远走到哪 
            {
                int k=j;
                while(h[j]<=h[k-1]) k=l[k-1];
                l[j]=k;
            }
            for(int j=m;j>=1;j--)//往右最远走到哪
            {
                int k=j;
                while(h[j]<=h[k+1]) k=r[k+1];
                r[j]=k;
            }
            for(int j=1;j<=m;j++)
            {
                if(h[j]&&!vis[l[j]][r[j]]&&(hv[i-1][j].L!=l[j]||hv[i-1][j].R!=r[j]))
                {
                    ans++;
                    vis[l[j]][r[j]]=1;
                }
                if(h[j])
                {
                    hv[i][j].L=l[j];
                    hv[i][j].R=r[j];
                }
            }
            for(int j=1;j<=m;j++)
            vis[l[j]][r[j]]=0;
        }
        cout<<ans<<endl;
        return 0;
    }



  • 相关阅读:
    原创《小白的Java自学课》第一课:Java是什么?Java到底能干嘛?
    谷歌chrome浏览器
    QT学习之QPair类
    char 与 signed char 和 unsigned char三者之间的关系
    QT学习之QT判断界面当前点击的按钮和当前鼠标坐标
    QT学习之QScript
    QT Creater 配色方案及下载
    QT学习之QString的arg方法
    QT创建与调用Dll方法(包括类成员)--显式调用
    C++学习之显式类型转换与运行时类型识别RTTI
  • 原文地址:https://www.cnblogs.com/chen99/p/11335264.html
Copyright © 2011-2022 走看看