2019牛客暑期多校训练营(第二场)

Second Large Rectangle
在这里插入图片描述
在这里插入图片描述
题目大意:
给你一个N*M的01矩阵,问这个矩阵中存在的第二大的矩形面积是多少,要求矩形是由全1 组成的,注意,如果只存在一个这样的矩形,那么就输出0

分析:
算是提供一个思路吧,定义一个二维数组,通过这个二维数组来记录每一列连续的1的个数,然后再去枚举列

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<map>
typedef long long ll;
using namespace std;
const int INF=0x3f3f3f3f;
int dp[1010][1010];
int n,m;
int max1,max2;
struct node{
    int h,w;
    node (int _h=0,int _w=0):h(_h),w(_w){};
}num[1010];
void cmp(int x){//max1是最大,max2是第二大
//    cout<<x<<endl;
    if(x>max1){
        max2=max1;
        max1=x;
    }else{
        max2=max(max2,x);
    }
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    scanf("%d%d",&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];
       }
    }

    for(int i=1;i<=n;i++)
    {
        int top=0,res;
        for(int j=1;j<=m;j++){
            if(!dp[i][j]) {//说明这个一列没有办法用了,那么就需要去从新去获得矩形的宽
                top=0;
                continue;
            }
            //下面这三行算是核心的算法了
            res=j;//不是从1开始,应是从当前的列开始的
            while(num[top].h>dp[i][j]&&top) res=num[top--].w;//构造出来的num数组是从小到大的数组
            if(dp[i][j]!=num[top].h) num[++top]=node(dp[i][j],res);//增加新的宽度

            for(int k=1;k<=top;k++){
                cmp(num[k].h*(j-num[k].w+1));
            }
        }
    }
    printf("%d\n",max2);
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值