All-one Matrices

All-one Matrices

单调栈

最大全一矩阵计数

#include<bits/stdc++.h>
#define maxn 3005
using namespace std;
#define P pair<int,int>
int n,m,ans,H[maxn][maxn],pre[maxn][maxn];
stack<P>st;
int a[maxn][maxn];
char c[maxn];//[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; j++)
        {
            H[i][j]=((a[i][j])?H[i-1][j]+1:0);
            pre[i][j]=pre[i][j-1]+(a[i][j]);
            //cout<<pre[i][j]<<' ';
        }
        pre[i][m+1]=pre[i][m];
        //cout<<endl;
    }
    int x;
    for(int i=1;i<=n;i++){
        while(!st.empty())st.pop();
        for(int j=1;j<=m+1;j++){
                x=j;
            while(!st.empty()&&st.top().first>H[i][j]){
                if(i==n||(pre[i+1][j-1]-pre[i+1][st.top().second-1])<(j-st.top().second)){
                    ans++;
                }
                x=st.top().second;
                st.pop();
            }
            if((st.empty()||H[i][j]>st.top().first))
            {

                st.push(P(H[i][j],x));///以Hij为高的矩形最长能向左延伸到x
            }
        }
    }
    printf("%d",ans);
}

 Second Large Rectangle

第二大全一矩阵,类似,单调栈,维护第二大

#include<bits/stdc++.h>
#define maxn 1005
using namespace std;
#define P pair<int,int>
int n,m,ans,H[maxn][maxn],pre[maxn][maxn];
stack<P>st;
int a[maxn][maxn];
char c[maxn];//[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; j++)
        {
            H[i][j]=((a[i][j])?H[i-1][j]+1:0);
            pre[i][j]=pre[i][j-1]+(a[i][j]);
            //  cout<<H[i][j]<<' ';
        }
        pre[i][m+1]=pre[i][m];
        //cout<<endl;
    }
    int x;
    int ma1=0,ma2=0;
    for(int i=1; i<=n; i++)
    {
        while(!st.empty())st.pop();
        for(int j=1; j<=m+1; j++)
        {
            x=j;
            while(!st.empty()&&st.top().first>H[i][j])
            {
                if(i==n||(pre[i+1][j-1]-pre[i+1][st.top().second-1])<(j-st.top().second))
                {
                    int a=j-st.top().second;
                    int b=st.top().first;
                    //cout<<i<<' '<<j<<endl;

                    if(a*b>=ma1)
                    {
                        ma2=max(max(ma1,max(a*(b-1),(a-1)*b)),ma2);
                        ma1=a*b;
                    }
                    else
                    {
                        ma2=max(ma2,a*b);
                    }
                    //ans++;
                }
                x=st.top().second;
                st.pop();
            }
            if(H[i][j]&&(st.empty()||H[i][j]>st.top().first))
            {
                st.push(P(H[i][j],x));
            }
        }
    }
    printf("%d\n",ma2);
}

 

posted @ 2019-08-16 11:22  liulex  阅读(163)  评论(0编辑  收藏  举报