Matrix Swapping II
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1861 Accepted Submission(s): 1239
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.
思路:
我们处理出以第i行为基础水平线的向下延展的柱形高度(以样例1为基础)sum【i】【j】:
2 0 1 3
1 0 0 2
0 0 0 1
然后我们以每一行为枚举量,去sort这一行的sum【i】【j】(从大到小);
那么ans=max(ans,j*sum【i】【j】);
Ac代码:
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
char a[1005][1005];
int sum[1005][1005];
int vis[1005];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(sum,0,sizeof(sum));
for(int i=0;i<n;i++)
{
scanf("%s",a[i]);
}
for(int i=0;i<m;i++)
{
for(int j=n-1;j>=0;j--)
{
if(a[j][i]=='1')
{
sum[j][i]=sum[j+1][i]+1;
}
}
}
int output=0;
for(int i=0;i<n;i++)
{
memset(vis,0,sizeof(vis));
for(int j=0;j<m;j++)
{
vis[sum[i][j]]++;
}
int tmp=0;
for(int j=n;j>=1;j--)
{
tmp+=vis[j];
output=max(output,tmp*j);
}
}
printf("%d\n",output);
}
}
本文介绍了一个算法问题,目标是通过交换矩阵中的列来获得最大的由1组成的矩形区域。文章详细解释了问题背景、输入输出格式及示例,并提供了一种解决方案,包括如何计算每个位置向下延伸的1的数量,以及如何通过排序这些数量来找到最优解。
692

被折叠的 条评论
为什么被折叠?



