2014
02-17

# Matrix Swapping II

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.

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

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

3 4
1011
1001
0001
3 4
1010
1001
0001

4
2

Note: Huge Input, scanf() is recommended.

给定一个N*M的01矩阵，在可以交换列的情况下，求出最大的全1的子矩阵。

代码如下：

#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#define MAXN 1005
using namespace std;

int N, M, high[MAXN], temp[MAXN];

char G[MAXN][MAXN];

int deal()
{
int Max = 0;
memcpy(temp, high, sizeof (high));
sort(temp+1, temp+M+1);
for (int i = M;  i >= 1; --i) {
if (temp[i]) {
Max = max((M-i+1)*temp[i], Max);
}
}
return Max;
}

int DP()
{
int Max = 0;
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= M; ++j) {
if (G[i][j] == '1') {
++high[j];
}
else {
high[j] = 0;
}
}
Max = max(Max, deal());
}
return Max;
}

int main()
{
while (scanf("%d %d", &N, &M) == 2) {
memset(high, 0, sizeof (high));
for (int i = 1; i <= N; ++i) {
scanf("%s", G[i]+1);
}
printf("%d\n", DP());
}
return 0;
}

1. 博主您好，这是一个内容十分优秀的博客，而且界面也非常漂亮。但是为什么博客的响应速度这么慢，虽然博客的主机在国外，但是我开启VPN还是经常响应很久，再者打开某些页面经常会出现数据库连接出错的提示