zoukankan      html  css  js  c++  java
  • Largest Submatrix of All 1’s(思维+单调栈)

    Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest we mean that the submatrix has the most elements.

    Input

    The input contains multiple test cases. Each test case begins with m and n (1 ≤ mn ≤ 2000) on line. Then come the elements of a (0,1)-matrix in row-major order on mlines each with n numbers. The input ends once EOF is met.

    Output

    For each test case, output one line containing the number of elements of the largest submatrix of all 1’s. If the given matrix is of all 0’s, output 0.

    Sample Input

    2 2
    0 0
    0 0
    4 4
    0 0 0 0
    0 1 1 0
    0 1 1 0
    0 0 0 0

    Sample Output

    0
    4 
    

    题意:

    找最大的为1的子矩阵,

    一开始的错误代码:H是表示他的连续高度,L和R是左边第一个比他小的坐标和右边比他小的坐标,这个过程都是单调栈维护的,

    然后暴力枚举最大的点,但是这样有错误,就是L到R这个区间内H的值不一定是相同的

    错误代码:

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<map>
    #include<set>
    #include<vector>
    #include<cmath>
    
    const int maxn=1e5+5;
    typedef long long ll;
    using namespace std;
    int n,m;
    int Map[2005][2005];
    int H[2005][2005];
    int L[2005][2005];
    int R[2005][2005];
    int  main()
    {
    	std::ios::sync_with_stdio(false);
        std::cin.tie(0);
        while(cin>>n>>m)
        {
        	for(int t=1;t<=n;t++)
        	{
        		for(int j=1;j<=m;j++)
        		{
        			scanf("%d",&Map[t][j]);
    			}
    		}
    		for(int t=1;t<=n;t++)
    		{
    			for(int j=1;j<=m;j++)
    			{
    				if(Map[t][j]==1)
    				{
    					H[t][j]=1;
    				 } 
    				 else
    				 {
    				 	H[t][j]=0;
    				 }
    			}
    		}
    		memset(L,0,sizeof(L));
    		for(int t=1;t<=n;t++)
    		{
    			for(int j=1;j<=m;j++)
    			{
    				R[t][j]=m+1;
    			}
    		}
    		for(int t=2;t<=n;t++)
    		{
    			for(int j=1;j<=m;j++)
    		    {
    		    	if(H[t-1][j]!=0&&H[t][j]==1)
    		    	{
    		    		H[t][j]=H[t-1][j]+1;
    				}
    		    }
    		}
    	    for(int t=1;t<=n;t++)
    	    {
    	    	stack<int>S1;
    	    	for(int j=1;j<=m;j++)
    	    	{
    	    		  while(S1.size() && Map[t][S1.top()]>=Map[t][j]) S1.pop();
                      if(S1.empty())    L[t][j]= 0;
                      else   L[t][j] = S1.top();
                      S1.push(j);
    			}
    		}
    		 for(int t=1;t<=n;t++)
    	    {
    	    	stack<int>S1;
    	    	for(int j=m;j>=1;j--)
    	    	{
    	    		  while(S1.size() && Map[t][S1.top()]>=Map[t][j]) S1.pop();
                      if(S1.empty())     R[t][j]= m+1;
                      else               R[t][j] = S1.top();
                      S1.push(j);
    			}
    		}
    		int manxx=0;
    		
    		for(int t=1;t<=n;t++)
    		{
    			
    			for(int j=1;j<=m;j++)
    			{
    			    if(H[t][R[t][j]-1]==H[t][L[t][j]+1])  
    				manxx=max(manxx,H[t][j]*(R[t][j]-L[t][j]-1));
    			}
    		}
    		cout<<manxx<<endl;
    	}
        return 0;
    }
    

    AC代码:

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<set>
    #include<vector>
    #include<cmath>
    const int maxn=2e5+5;
    typedef long long ll;
    using namespace std;
    int H[2005];
    int a[2005];
    int main()
    {
       	std::ios::sync_with_stdio(false);
        std::cin.tie(0);
        int n,m,x,top,tmp,maxnxx;
        stack<int>S1; 
        while(cin>>n>>m)  
        {  
            maxnxx=0;  
            memset(H,0,sizeof(H)); 
            for(int t=0;t<n;t++)  
            {  
                for(int j=1;j<=m;j++)  
                {  
                    scanf("%d",&x); 
                    if(x==1) H[j]=H[j]+1;    
                    else H[j]=0;
                    a[j]=H[j]; 
                }  
                a[m+1]=-1; 
                for(int j=1;j<=m+1;j++)  
                {  
                    if(S1.empty()||a[j]>=a[S1.top()])  
                    { 
                        S1.push(j);  
                    }  
                    else  
                    {  
                        while(!S1.empty()&&a[j]<a[S1.top()])  
                        {    
                            top=S1.top();  
                            S1.pop();  
                            tmp=(j-top)*a[top]; 
                            if(tmp>maxnxx) maxnxx=tmp; 
                        }  
                        S1.push(top);    
                        a[top]=a[j]; 
                    }  
                }  
            }  
            cout<<maxnxx<<endl; 
        }
       return 0;
    }
    
  • 相关阅读:
    NopCommerce代码结构
    至于你信不信,我反正是信了——以类为单位的编程思想
    男人的小金库藏在哪里?——公共数据集Model
    老婆使用说明书——注册系统的逻辑与结构
    ASP.NET学习参考站点
    学了N年英语,你学会翻译了吗?——最基本的数据库连接
    编程也讲禅,您读过《金刚经》吗?——ADO.NET核心类的灭度与SQLHelper的诞生——十八相送(上)
    文件夹病毒专杀工具
    不想当将军的学生,不是好程序员——数据访问层DAL——程序结构的思考
    html的标签一共有多少个?
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781765.html
Copyright © 2011-2022 走看看