http://codeforces.com/problemset/problem/375/B
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 ≤ u; l ≤ 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
Copy
1 1
1
Output
Copy
1
Input
Copy
2 2
10
11
Output
Copy
2
Input
Copy
4 3
100
011
000
101
Output
Copy
2
题目大意:给你一个0、1组成的矩阵,大小为 n × m ,你可以把任意的两行的位置相互交换,请你给出变换后能得到的最大子矩阵,满足只由“1”组成。
思路: 既然可以交换任意两行的位置, 我们考虑枚举列,即从第1列第2列……开始把从该列开始向前数的所有行的连续的1的个数进行排序, 然后从大到小遍历并记录最大值。画个图吧, 方便大家理解:
以从第4列向前看为例, 下面是排序后的结果:
可以看到, 我们从最大值向最小值遍历, 如图中有三个符合题意的矩阵,我们记录最大值即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
int a[5005][5005];
int b[5005];
char s[5005];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int MAX=0;
int temp1=0;
int temp2=0;
int temp3=0;
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
{
scanf("%s",s);
for(int j=1;j<=m;j++)
{
if(s[j-1]=='0')
a[i][j]=0;
else
a[i][j]=a[i][j-1]+1;//记录从第i行第j列向前数的连续的1的个数
}
}
for(int i=1;i<=m;i++)//从第一列 第二列……向左边看
{
temp1=temp2=0;
temp3=5005;
for(int j=0;j<n;j++)
{
if(a[j][i]!=0)
{
if(temp1<a[j][i])
temp1=a[j][i];//记录最大值
if(temp3>a[j][i])
temp3=a[j][i];//记录最小值
b[a[j][i]]++;//计数排序
}
}
while(temp1>=temp3)
{
if(b[temp1])
{
temp2+=b[temp1];
if(temp2*temp1>MAX)
MAX=temp2*temp1;//记录最大值
b[temp1]=0;
}
temp1--;
}
}
printf("%d\n",MAX);
return 0;
}