2019牛客暑期多校训练营(第二场)- H - Second Large Rectangle

题目描述:

Given a N×MN×M binary matrix. Please output the size of second large rectangle containing all "1""1".

Containing all "1""1" means that the entries of the rectangle are all "1""1".

A rectangle can be defined as four integers  x1,y1,x2,y2x1,y1,x2,y2 where 1x1x2N1≤x1≤x2≤N and 1y1y2M1≤y1≤y2≤M. Then, the rectangle is composed of all the cell (x, y) where x1xx2x1≤x≤x2 and y1yy2y1≤y≤y2. If all of the cell in the rectangle is "1""1", this is a valid rectangle.

Please find out the size of the second largest rectangle, two rectangles are different if exists a cell belonged to one of them but not belonged to the other.
 
输入描述:

The first line of input contains two space-separated integers N and M.
Following N lines each contains M characters cijcij.

1N,M10001≤N,M≤1000
N×M2N×M≥2
cij"01"cij∈"01"
 
输出描述:
 
Output one line containing an integer representing the answer. If there are less than 2 rectangles containning all "1""1", output "0""0".
 
样例:

题目大意:给一个n*m的01矩阵,求所有全为1组成的矩阵中第二大的1矩阵的面积,如果1矩阵不足2个,输出0。

解题代码:

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
const int _ = 1005;
int dp[_][_];
int secArea = -1,maxArea = -1;
int main()
{
    int n,m;
    memset(dp,0,sizeof(dp));
    cin>>n>>m;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            scanf("%1d",&dp[i][j]);
            dp[i][j]+=dp[i][j]*dp[i-1][j];
        }
    }//预处理dp[i][j]为该点上方路连续1的最大值
    for(int i = 1; i<=n; i++)
    {
        for(int j = 1; j<=m; j++)
        {
            int height = dp[i][j];
            int wide = 1;
            //当前矩阵的高和宽
            while(height)
            {
                int Area = height*wide;
                if(Area>maxArea)
                {
                    secArea = maxArea;
                    maxArea = Area;
                }
                else if(Area>secArea)
                {
                    secArea = Area;
                }
                height = min(height,dp[i][j-wide]);
                wide++;
            }
        }
    }
    cout<<(secArea == -1 ? 0 : secArea)<<endl;
    return 0;

}

 

转载于:https://www.cnblogs.com/cloudplankroader/p/11226752.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值