您的位置:首页 > 移动开发

HDU 2830 Matrix Swapping II

2014-07-29 22:52 351 查看
给一个矩阵,依然是求满足条件的最大子矩阵

不过题目中说任意两列可以交换,这是对题目的简化

求出h数组以后直接排序,然后找出(col-j)*h[j]的最大值即可(这里的j是从0开始)

因为排序会影响到h数组下一行的求解,所以将h数组中的元素复制到temp数组中去,再排序

//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 1010;
char map[maxn][maxn];
int h[maxn], temp[maxn];

int main(void)
{
#ifdef LOCAL
freopen("2830in.txt", "r", stdin);
#endif

int row, col;
while(scanf("%d%d", &row, &col) == 2)
{
int i, j;
for(i = 0; i < row; ++i)
scanf("%s", map[i]);
memset(h, 0, sizeof(h));
int ans = 0;
for(i = 0; i < row; ++i)
{
for(j = 0; j < col; ++j)
{
if(map[i][j] == '1')
++h[j];
else
h[j] = 0;
}
memcpy(temp, h, sizeof(h));
sort(temp, temp + col);
for(j = 0; j < col; ++j)
ans = max(ans, (col-j)*temp[j]);
}
printf("%d\n", ans);
}
return 0;
}


代码君
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: