求01矩阵最大面积Maximal submatrix HDU - 6957

Given a matrix of n rows and m columns,find the largest area submatrix which is non decreasing on each column

Input

The first line contains an integer T(1≤T≤10)

representing the number of test cases.
For each test case, the first line contains two integers n,m(1≤n,m≤2∗103)representing the size of the matrix
the next n line followed. the i-th line contains m integers vij(1≤vij≤5∗103)representing the value of matrix
It is guaranteed that there are no more than 2 testcases with n∗m>10000

Output

For each test case, print a integer representing the Maximal submatrix

Sample Input

1
2 3
1 2 4
2 3 3

Sample Output

4

题目大意

求矩阵的最大面积 其中必须要满足a[i][j]>=a[i-1][j];

思路

1将数组转换为01数组 然后用悬线法计算面积

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=2e3+10;
int t,n,m;
int mp[maxn][maxn];
int le[maxn][maxn];
int r[maxn][maxn];
int h[maxn][maxn];
int mi(int x,int y)
{
	if(x<y)
	return x;
	else return y;
}
int ma(int x,int y)
{
	if(x<y)
	return y;
	else return x;
}
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		
		memset(le,0,sizeof(le));
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				
				le[0][j]=0;
				scanf("%d",&mp[i][j]);
				if(i!=1)
				if(mp[i][j]>=mp[i-1][j])
				{
					le[i][j]=1;
					
				}
				//else le[i][j]=1;
			}
		}
		memset(mp,0,sizeof(mp));
		memset(h,0,sizeof(h));
		memset(r,0,sizeof(r));
		int ans=m;
		int L=1e9,R=0;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				if(le[i][j]==1)
				{
					L=mi(L,j);
				}
				else L=1e9;
				mp[i][j]=L;
			}
			for(int j=m;j>=1;j--)
			{
				if(le[i][j]==1)
				{
					R=ma(R,j);
				}
				else R=0;
				r[i][j]=R;
			}
			for(int j=1;j<=m;j++)
			{
				if(le[i-1][j]==1)
				{
					h[i][j]=h[i-1][j]+1;
					mp[i][j]=ma(mp[i-1][j],mp[i][j]);
					r[i][j]=mi(r[i-1][j],r[i][j]);
				}
				else h[i][j]=1;
				if(le[i][j]==1)
				{
					ans=max(ans,(r[i][j]-mp[i][j]+1)*(h[i][j]+1));
				}
			}
		 } 
		cout<<ans<<endl;
	}
	return 0;
}

2 逐行 用单调栈计算最大面积

#include<bits/stdc++.h>
using namespace std;
int t;
int n,m,k;
int top;
const int maxn =2e3+10;
int h[maxn],b[maxn];
int a[maxn][maxn];
int main()
{
	cin>>t;
	while(t--)
	{
		stack<int >s;
		int ans=0;
		memset(h,0,sizeof(h));
        memset(b,0,sizeof(b));
		cin>>n>>m;
		for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                scanf("%d",&a[i][j]);
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				if(a[i][j]>=a[i-1][j])
				{
					h[j]++;
				}
				else h[j]=1;
				b[j]=h[j];
			}
			b[m+1]=-1;
			for(int j=1;j<=m+1;j++)
			{
				if(s.empty()||b[s.top()]<=b[j])
				{
					s.push(j);
					continue;
				}
				while(!s.empty()&&b[s.top()]>b[j])
				{
					top=s.top();
					s.pop();
					int tmp=(j-top)*b[top];
					ans=max(ans,tmp);
				}
				s.push(top);
				b[top]=b[j];//将第一个 大于j的值的位置(top)再放进去 但是值要改变
			}
		} 
		cout<<ans<<endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是非递归实现任意柱状图最大矩形面积的C语言代码: ```c #include <stdio.h> #include <stdlib.h> #include <limits.h> // 定义栈结构体 typedef struct { int top; // 栈顶指针 int* arr; // 存储栈元素的数组 } Stack; // 创建一个栈 Stack* create(int size) { Stack* s = (Stack*)malloc(sizeof(Stack)); s->top = -1; s->arr = (int*)malloc(sizeof(int) * size); return s; } // 判断栈是否为空 int isEmpty(Stack* s) { return s->top == -1; } // 判断栈是否已满 int isFull(Stack* s, int size) { return s->top == size - 1; } // 入栈 void push(Stack* s, int x) { s->arr[++s->top] = x; } // 出栈 int pop(Stack* s) { return s->arr[s->top--]; } // 获取栈顶元素 int peek(Stack* s) { return s->arr[s->top]; } // 最大矩形面积 int largestRectangleArea(int* heights, int heightsSize) { Stack* s = create(heightsSize + 1); // 创建栈,栈的大小为 heightsSize + 1 int maxArea = 0; // 最大矩形面积 int i = 0; // 遍历 heights 数组的指针 while (i < heightsSize || !isEmpty(s)) { if (isEmpty(s) || heights[i] >= heights[peek(s)]) { push(s, i++); // 如果栈为空或当前元素大于等于栈顶元素,将当前元素入栈 } else { int top = pop(s); // 如果当前元素小于栈顶元素,弹出栈顶元素 int width = isEmpty(s) ? i : i - peek(s) - 1; // 计算矩形宽度 int area = heights[top] * width; // 计算矩形面积 if (area > maxArea) { maxArea = area; // 更新最大矩形面积 } } } // 释放栈占用的内存 free(s->arr); free(s); return maxArea; } // 矩阵中最大矩形面积 int maximalRectangle(char** matrix, int matrixSize, int* matrixColSize) { if (matrixSize == 0 || matrixColSize == NULL || matrixColSize[0] == 0) { return 0; } int n = matrixColSize[0]; // 矩阵的列数 int* heights = (int*)malloc(sizeof(int) * n); // 存储每一行的高度 int maxArea = 0; // 最大矩形面积 for (int i = 0; i < matrixSize; i++) { // 计算当前行的高度 for (int j = 0; j < n; j++) { if (matrix[i][j] == '0') { heights[j] = 0; } else { heights[j]++; } } // 计算以当前行为底边的最大矩形面积 int area = largestRectangleArea(heights, n); if (area > maxArea) { maxArea = area; // 更新最大矩形面积 } } // 释放 heights 数组占用的内存 free(heights); return maxArea; } int main() { char* matrix[] = {"10100", "10111", "11111", "10010"}; int matrixSize = sizeof(matrix) / sizeof(char*); int matrixColSize[] = {5, 5, 5, 5}; int maxArea = maximalRectangle(matrix, matrixSize, matrixColSize); printf("最大矩形面积:%d\n", maxArea); return 0; } ``` 该算法的时间复杂度为O(mn),空间复杂度为O(n),其中m为矩阵的行数,n为矩阵的列数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值