Matrix Swapping II
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1470 Accepted Submission(s): 977
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.
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
此题说是动态规划,已经和传统的动规很不同了。我觉得倒是有点像分治,先每一行计算能往下延伸到没有1,然后对每一行排序,求出每一行面积的最大值,然后所有行的最大值,就是结果。部分处理类似HDU 1505
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
//#define LOCAL
using namespace std;
int n,m;
int ans = 0;
char a[1020][1020];
int dp[1020][1020];
bool cmp(int a,int b){
return a>b;
}
int main()
{
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif // LOCAL
while(scanf("%d%d",&m,&n)!=EOF){
int i,j;
for(i = 0;i<m;i++)
scanf("%s",a[i]);
for(i = 0;i<n;i++)
dp[0][i] = a[0][i] - '0';
for(i = 1;i<m;i++)
for(j = 0;j<n;j++)
if(a[i][j] == '0')
dp[i][j] = 0;
else
dp[i][j] = dp[i-1][j] + 1;
int Max = 0;
int term;
for(i = 0;i<m;i++){
sort(dp[i],dp[i]+n,cmp);
for(j = 0;j<n;j++){
term = dp[i][j]*(j+1);
if(term>Max)
Max = term;
}
}
printf("%d\n",Max);
}
return 0;
}