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 ≤ m, n ≤ 2000) on line. Then come the elements of a (0,1)-matrix in row-major order on m lines 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
#include<stdio.h>
#include<string.h>
#include<stack>
//以后还是从1开始写吧,有些东西感觉还是这么写比较简单

//写一下思路,当然也是看了别人的题解和代码,慢慢的做出来的

//刚才不会写代码,1.不知道怎么记录这个高度,主要他还不一定是连续的,这简直就是要命啊,一头雾水
//解决方法,就是设立一个h[][]的二维的数组,这样的话,只要这个点的map[][]是0,就是说明这里没有高度
//如果这里是一的话,我们就可以让它加上上面一的个数,这样的处理还是很厉害的,当然还有就是这样的话,
//我们的数组就不能从00开始了,这样的话,会导致我们第一行没有办法写代码、

//还有就是一行中有断开的,怎么处理,根本没有头绪,
//解决办法,就是一行一行的找,这样的话,就可以依次遍历,这一行中的底都是相同的,这样就可以处理数据了,
//只需要保留着最大值就好了,还有最大值可以直接用max(),
//还是很方便的
using namespace std;
stack <int> stk;
const int Max=2000+10;
int mp[Max][Max];
int m,n;
int h[Max][Max];//记录每一列中连续1的高度
int l[Max],r[Max];
int main()
{

   memset(mp,0,sizeof(mp));
   memset(h,0,sizeof(mp));
   while(~scanf("%d %d",&m,&n))
   {   int ans=0;
       for(int i=1;i<=m;i++)
        for(int j=1;j<=n;j++)
        {
           scanf("%d",&mp[i][j]);
           h[i][j]= mp[i][j]==0?0:h[i-1][j]+1;//逐行记录高度每一列的高度
        }
      for(int i=1;i<=m;i++)//逐行考虑高度,每个小矩形
      {
          stack<int> stk1;//这个地方的优势还是很大的,不用清空栈节省了时间,不过这样的话,就不能全局变量
                          //将函数的功能抽出来了,各有优缺点
          for(int j=1;j<=n;j++)
          {
              while(!stk1.empty()&&h[i][j]<=h[i][stk1.top()])
                stk1.pop();
              l[j]=stk1.empty()? 1: stk1.top()+1;
              stk1.push(j);
          }
          stack<int> stk2;
          for(int j=n;j>=1;j--)
          {
              while(!stk2.empty()&&h[i][j]<=h[i][stk2.top()])
                stk2.pop();
              r[j]=stk2.empty()?n:stk2.top()-1;
              stk2.push(j);
          }
          for(int j=1;j<=n;j++)
          {
              int s;
              s=h[i][j]*(r[j]-l[j]+1);
              ans=max(ans,s);
          }
      }
      printf("%d\n",ans);
   }
   return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用C语言解决下列问题:Kirill wants to weave the very beautiful blanket consisting of n×m of the same size square patches of some colors. He matched some non-negative integer to each color. Thus, in our problem, the blanket can be considered a B matrix of size n×m consisting of non-negative integers. Kirill considers that the blanket is very beautiful, if for each submatrix A of size 4×4 of the matrix B is true: A11⊕A12⊕A21⊕A22=A33⊕A34⊕A43⊕A44, A13⊕A14⊕A23⊕A24=A31⊕A32⊕A41⊕A42, where ⊕ means bitwise exclusive OR Kirill asks you to help her weave a very beautiful blanket, and as colorful as possible! He gives you two integers n and m . Your task is to generate a matrix B of size n×m , which corresponds to a very beautiful blanket and in which the number of different numbers maximized. Input The first line of input data contains one integer number t (1≤t≤1000 ) — the number of test cases. The single line of each test case contains two integers n and m (4≤n,m≤200) — the size of matrix B . It is guaranteed that the sum of n⋅m does not exceed 2⋅105 . Output For each test case, in first line output one integer cnt (1≤cnt≤n⋅m) — the maximum number of different numbers in the matrix. Then output the matrix B (0≤Bij<263) of size n×m . If there are several correct matrices, it is allowed to output any one. It can be shown that if there exists a matrix with an optimal number of distinct numbers, then there exists among suitable matrices such a B that (0≤Bij<263) .
03-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值