Codeforces Round #221 (Div. 2)-D. Maximum Submatrix 2

原題鏈接


D. Maximum Submatrix 2
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given a matrix consisting of digits zero and one, its size is n × m. You are allowed to rearrange its rows. What is the maximum area of the submatrix that only consists of ones and can be obtained in the given problem by the described operations?

Let's assume that the rows of matrix a are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. A matrix cell on the intersection of the i-th row and the j-th column can be represented as (i, j). Formally, a submatrix of matrix a is a group of four integers d, u, l, r (1 ≤ d ≤ u ≤ n; 1 ≤ l ≤ r ≤ m). We will assume that the submatrix contains cells (i, j)(d ≤ i ≤ ul ≤ j ≤ r). The area of the submatrix is the number of cells it contains.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 5000). Next n lines contain m characters each — matrix a. Matrix a only contains characters: "0" and "1". Note that the elements of the matrix follow without any spaces in the lines.

Output

Print a single integer — the area of the maximum obtained submatrix. If we cannot obtain a matrix of numbers one, print 0.

Examples
input
1 1
1
output
1
input
2 2
10
11
output
2
input
4 3
100
011
000
101
output
2

用str[][]存儲整個矩陣

d[i][j]表示str[i][j]爲'1'的情況下,第i行以str[i][j]爲結尾的連續的最長的1的長度(若str[i][j] == '1')d[i][j] = d[i][j-1] + 1

遍歷d[][]的每一列執行操作vis[d[i][j]]++, 求每個長度的個數,遍歷完一列後,從後到前執行vis[i-1] += vis[i]

更新ans = max(ans, vis[i]*i)

#include <bits/stdc++.h>
#define maxn 5005
#define MOD 1000000007
typedef long long ll;
using namespace std;

char str[maxn][maxn];
int d[maxn][maxn];
int vis[maxn];
int main() {
	
//	freopen("in.txt", "r", stdin);
	int n, m;
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i++)
	 scanf("%s", str[i]+1);
	for(int i = 1; i <= n; i++)
	 for(int j = 1; j <= m; j++) {
	 	if(str[i][j] == '1') {
	 		d[i][j] = d[i][j-1] + 1;
	 	}
	}
	int ans = 0;
	for(int i = 1; i <= m; i++){
	 memset(vis, 0, sizeof(vis));
	 for(int j = 1; j <= n; j++) {
	 	vis[d[j][i]]++;
	 }
	 for(int j = 5000; j >= 1; j--) {
	 	vis[j-1] += vis[j];
	 	ans = max(ans, vis[j] * j);
	 }
	}
	printf("%d\n", ans);
	return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章