CodeForces 375 B. Maximum Submatrix 2

本文介绍了一种通过重新排列矩阵的行来寻找由1组成的最大子矩阵面积的方法。使用特定算法,通过对输入矩阵进行处理并排序,最终找到该子矩阵的最大面积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写法似乎太暴力了。。。

B. Maximum Submatrix 2
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given a matrix consisting of digits zero and one, its size is n × m. You are allowed to rearrange its rows. What is the maximum area of the submatrix that only consists of ones and can be obtained in the given problem by the described operations?

Let's assume that the rows of matrix a are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. A matrix cell on the intersection of the i-th row and the j-th column can be represented as (i, j). Formally, a submatrix of matrix ais a group of four integers d, u, l, r (1 ≤ d ≤ u ≤ n; 1 ≤ l ≤ r ≤ m). We will assume that the submatrix contains cells (i, j) (d ≤ i ≤ ul ≤ j ≤ r). The area of the submatrix is the number of cells it contains.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 5000). Next n lines contain m characters each — matrix a. Matrix a only contains characters: "0" and "1". Note that the elements of the matrix follow without any spaces in the lines.

Output

Print a single integer — the area of the maximum obtained submatrix. If we cannot obtain a matrix of numbers one, print 0.

Sample test(s)
input
1 1
1
output
1
input
2 2
10
11
output
2
input
4 3
100
011
000
101
output
2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

char map[5500][5500];
int h[5500][5500],n,m,ans=0;

bool cmp(int a,int b)
{
    return a>b;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++) scanf("%s",map[i]);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(map[i-1][j-1]=='1') h[j][i]=h[j-1][i]+1;
        }
    }
    for(int i=1;i<=m;i++)
    {
        sort(h[i]+1,h[i]+n+1,cmp);
        for(int j=1;j<=n;j++)
        {
            if(h[i][j]==0) break;
            ans=max(ans,h[i][j]*j);
        }
    }
    printf("%d\n",ans);
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值