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

 

 

### 获取 C# 中 Matrix4x4 的子矩阵方法 在 C# 中,`Matrix4x4` 是一种用于表示 4×4 矩阵的数据结构,通常应用于图形学领域中的变换操作。然而,标准库并未提供直接提取子矩阵的功能。为了实现这一需求,可以通过手动计算来构建一个新的 `Matrix4x4` 或者其他形式的矩阵。 以下是通过代码实现从 `Matrix4x4` 提取子矩阵的一种方式: #### 方法描述 要从 `Matrix4x4` 提取子矩阵,可以定义一个函数,该函数接受原始矩阵以及指定行列范围作为参数,并返回新的矩阵对象。由于 `Matrix4x4` 并不支持动态大小调整,因此可能需要借助二维数组或其他数据结构完成中间处理过程[^1]。 ```csharp using System; public class MatrixOperations { public static float[,] GetSubMatrix(Matrix4x4 matrix, int startRow, int endRow, int startCol, int endCol) { if (startRow < 0 || endRow >= 4 || startCol < 0 || endCol >= 4 || startRow > endRow || startCol > endCol) throw new ArgumentException("Invalid range specified."); int rows = endRow - startRow + 1; int cols = endCol - startCol + 1; float[,] result = new float[rows, cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result[i, j] = matrix[startRow + i, startCol + j]; } } return result; } } ``` 需要注意的是,在实际应用中,`Matrix4x4` 类型并不像某些高级线性代数库那样提供了索引器访问功能。如果目标平台允许扩展,则可通过反射机制或者自定义封装类模拟这种行为[^2]。 另外,当仅需部分列或行时,也可以考虑投影到更简单的矢量类型(如 Vector3/Vector4),从而减少不必要的内存占用和复杂度提升效率[^3]。 #### 示例代码展示如何调用此方法: 假设我们有一个单位矩阵并希望获得其左上方的一个 2x2 子区域: ```csharp var identity = Matrix4x4.Identity; float[,] subMatrix = MatrixOperations.GetSubMatrix(identity, 0, 1, 0, 1); // 打印结果验证 for(int i=0;i<subMatrix.GetLength(0);i++) { Console.WriteLine(string.Join(", ", Enumerable.Range(0,(int)subMatrix.GetLength(1)).Select(j=>subMatrix[i,j]))); } ``` 以上展示了基本逻辑框架;具体实现细节可能会依据项目环境有所变化,请根据实际情况适当修改适应特定场景下的性能优化需求[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lbperfect123

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值