Matrix Swapping II
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1396 Accepted Submission(s): 924
Problem Description
Given an N * M matrix with each entry equal to 0 or 1. We can find some rectangles in the matrix whose entries are all 1, and we define the maximum area of such rectangle as this matrix’s goodness.
We can swap any two columns any times, and we are to make the goodness of the matrix as large as possible.
We can swap any two columns any times, and we are to make the goodness of the matrix as large as possible.
Input
There are several test cases in the input. The first line of each test case contains two integers N and M (1 ≤ N,M ≤ 1000). Then N lines follow, each contains M numbers (0 or 1), indicating the N * M matrix
Output
Output one line for each test case, indicating the maximum possible goodness.
Sample Input
3 4 1011 1001 0001 3 4 1010 1001 0001
Sample Output
4 2 Note: Huge Input, scanf() is recommended.
这个求1所围城的矩阵方法很有意思,每一行每一行的处理,如果是1的话,就++,否则为零,然后从大到小排序,注意要赋给另一个数组让另一个数组排序,不然就乱了,
然后在num[i]*i,找最大的。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char map[1000+5][1000+5];
int mm[1000+5];
int ss[1000+5];
int cmp(int x,int y)
{
return x>y;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
getchar();
int ans=0;
memset(mm,0,sizeof(mm));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
scanf("%c",&map[i][j]);
getchar();
for(int j=1;j<=m;j++)
{
if(map[i][j]=='1') mm[j]++;
else mm[j]=0;
ss[j]=mm[j];
}
// for(int i=1;i<=m;i++)
// cout<<mm[i]<<" ";
//cout<<endl;
sort(ss+1,ss+m+1,cmp);
for(int i=1;i<=m;i++)
if(ss[i]*i>ans)
ans=ss[i]*i;
// cout<<ans<<endl;
}
printf("%d\n",ans);
/* for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
cout<<map[i][j];
cout<<endl;
}*/
}
return 0;
}