首页 > 其他 > 详细

All-one Matrices

时间:2019-08-16 12:28:59      阅读:77      评论:0      收藏:0      [点我收藏+]

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);
}

 

All-one Matrices

原文:https://www.cnblogs.com/liulex/p/11362763.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!