CF376 D. Maximum Submatrix 2

题目传送门:https://codeforces.com/problemset/problem/376/D

题目大意:
给你 \(n\times m\) 的01矩阵,问你在对行(Row)任意排序后,最大的全1子矩阵大小


因为每一行里面的相对位置不会发生改变,故我们预处理一下

记\(R[i][j]\)表示位置\((i,j)\)能向右能延伸多远

然后对于每个\(j\),我们按\(R[i][j]\)的值对\(1\sim n\)行排序

类似单调栈的思想往下找即可

/*program from Wolfycz*/
#include<map>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Fi first
#define Se second
#define ll_inf 1e18
#define MK make_pair
#define sqr(x) ((x)*(x))
#define pii pair<int,int>
#define int_inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
	static char buf[1000000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
template<typename T>inline T frd(T x){
	int f=1; char ch=gc();
	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')    f=-1;
	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
template<typename T>inline T read(T x){
	int f=1; char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
inline void print(int x){
	if (x<0)	putchar('-'),x=-x;
	if (x>9)	print(x/10);
	putchar(x%10+'0');
}
const int N=5e3;
int F[N+10][N+10];
char Map[N+10][N+10];
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	int n=read(0),m=read(0),Ans=0;
	for (int i=1;i<=n;i++)	scanf("%s",Map[i]+1);
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
			F[j][i]=!(Map[i][j]-'0')?0:F[j-1][i]+1;
	for (int j=1;j<=m;j++){
		sort(F[j]+1,F[j]+1+n);
		reverse(F[j]+1,F[j]+1+n);
		for (int i=1;i<=n;i++){
			if (!F[j][i])	break;
			Ans=max(Ans,F[j][i]*i);
		}
	}
	printf("%d\n",Ans);
	return 0;
}
上一篇:[Leetcode学习-java]Maximum Length of Repeated Subarray


下一篇:[LeetCode] 1186. Maximum Subarray Sum with One Deletion 删除一次得到子数组最大和