Largest Submatrix of All 1’s(单调队列)

Largest Submatrix of All 1’s

Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 9559 Accepted: 3413
Case Time Limit: 2000MS

Description

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

题目大意:
给你一个n*m的矩阵也要你求最大的全1矩阵中1的个数。

一开始的思路是bfs把子矩阵里面的1全部清0并且记录1的个数。可惜交叉的1处理不来例如:
5 7
0 0 1 1 0 0 0
0 0 1 1 0 1 1
1 1 1 1 0 0 0
1 1 1 1 0 0 0
0 0 0 0 0 0 0
最大应该是8,但是输出12
代码:

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;

int n,m;
const int maxn=2000+10;
bool a[maxn][maxn];
bool vis[maxn][maxn];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
struct node{
	int x,y;
	node(int a,int b):x(a),y(b){}
};
int bfs(int x,int y){
	int s=1;
	queue<node>st;
	vis[x][y]=1;
	a[x][y]=0;
	st.push(node(x,y));
	while(!st.empty()){
		node now=st.front();
		st.pop();
		for(int i=0;i<4;i++){
			int fx=now.x+dir[i][0];
			int fy=now.y+dir[i][1];
			if(a[fx][fy]==1&&!vis[fx][fy]){
				vis[fx][fy]=1;
				s++;
				a[fx][fy]=0;
				st.push(node(fx,fy));
			}
		}
	}
	return s;
}
int main(){
	while(scanf("%d%d",&n,&m)!=EOF){
		memset(a,false,sizeof(a));
		memset(vis,false,sizeof(vis));
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				scanf("%d",&a[i][j]);
			}
		}
		int _max1=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(a[i][j]==1){
					_max1=max(_max1,bfs(i,j));
				}
			}
		}
		printf("%d\n",_max1);
	}
}

参考:https://blog.csdn.net/zuzhiang/article/details/78136417
后来看了这篇blog写出来的,大概思路就是对每行维护一个最大高然后就是单调栈求最大面积了。(01矩阵转换柱形图过于巧妙)
代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn=2000+20;
int w[maxn],h[maxn];
struct Stack{
	int top;
	int data[maxn];
	Stack(){top=-1;}
	bool empty(){return top==-1;}
};

int ans;
void push(Stack *stack,int x){
	if(stack->empty()||x>stack->data[stack->top]){
		stack->data[++stack->top]=x;
		w[stack->top]=1;
	}else{
		int wid=0;
		while(!stack->empty()&&x<=stack->data[stack->top]){
			wid+=w[stack->top];
			ans=max(ans,wid*stack->data[stack->top]);
			stack->top--;
		}
		stack->data[++stack->top]=x;
		w[stack->top]=wid+1;
	}
}
int main(){
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF){
		ans=0;int x;
		memset(w,0,sizeof(w));
		memset(h,0,sizeof(h));
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				scanf("%d",&x);
				if(x==1){
					h[j]=h[j]+1;
				}else if(x==0){
					h[j]=0;
				}
			}
			Stack *stack=new Stack();
			for(int k=0;k<n;k++){
				push(stack,h[k]);
			}
			push(stack,0);
		}
		printf("%d\n",ans);
	}
} 
  • 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、付费专栏及课程。

余额充值