zoukankan      html  css  js  c++  java
  • 2019牛客多校第二场H题(悬线法)

    把以前的题补补,用悬线求面积第二大的子矩形。我们先求出最大子矩阵的面积,并记录其行三个方向上的悬线长度。然后排除这个矩形,记得还得特判少一行或者少一列的情况

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=1e3+10;
    int n;
    int mat[maxn][maxn],Left[maxn][maxn],Right[maxn][maxn],up[maxn][maxn];
    
    int main()
    {
        int T;
        T=1;
        while(T--)
        {
            int m,n;
            cin>>m>>n;
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<n;j++)
                {
                    char ch;
                    cin>>ch;
                    mat[i][j]=ch=='1'?0:1;
                }
            }
            int ans=0,max2=0,l,r,u,b;
            for(int i=0;i<m;i++)
            {
                int lo=-1,ro=n;
                for(int j=0;j<n;j++)
                {
                    if(mat[i][j]==1)
                    {
                        up[i][j]=Left[i][j]=0;lo=j;
                    }
                    else
                    {
                        up[i][j]=i==0?1:up[i-1][j]+1;
                        Left[i][j]=i==0?lo+1:max(Left[i-1][j],lo+1);
                    }
                }
                for(int j=n-1;j>=0;j--)
                {
                    if(mat[i][j]==1)
                    {
                        Right[i][j]=n;ro=j;
                    }
                    else
                    {
                        Right[i][j]=i==0?ro-1:min(Right[i-1][j],ro-1);
                        if(ans<(up[i][j]*(Right[i][j]-Left[i][j]+1)))
                        {
                            ans=up[i][j]*(Right[i][j]-Left[i][j]+1);
                            u=up[i][j],l=Left[i][j],r=Right[i][j],b=i;
                         } 
                    }
                }
            }
            max2=max(max2,u*(r-l));
            max2=max(max2,(u-1)*(r-l+1)); 
            for(int i=0;i<m;i++)
            {
                int lo=-1,ro=n;
                for(int j=0;j<n;j++)
                {
                    if(mat[i][j]==1)
                    {
                        up[i][j]=Left[i][j]=0;lo=j;
                    }
                    else
                    {
                        up[i][j]=i==0?1:up[i-1][j]+1;
                        Left[i][j]=i==0?lo+1:max(Left[i-1][j],lo+1);
                    }
                }
                for(int j=n-1;j>=0;j--)
                {
                    if(mat[i][j]==1)
                    {
                        Right[i][j]=n;ro=j;
                    }
                    else
                    {
                        Right[i][j]=i==0?ro-1:min(Right[i-1][j],ro-1);    
                        if(u==up[i][j]&&l==Left[i][j]&&r==Right[i][j]&&b==i)
                        continue;
                        if(max2<(up[i][j]*(Right[i][j]-Left[i][j]+1)))
                        {
                            max2=up[i][j]*(Right[i][j]-Left[i][j]+1);
                            
                         } 
                    }
                }
            }
            cout<<max2<<"
    ";
         } 
        return 0;
    }
  • 相关阅读:
    dynamic 类型不能访问属性
    chrome浏览器另存为/上传附件崩溃
    HTTP 错误 405.0
    2019目录之电影
    面向面试编程-概念之-分布式与集群的区别和联系
    面向面试编程代码片段之GC
    2018-12-24
    VUE iview date-picker取时间范围...
    关闭shift中英文切换 英文代码/中文注释随意切换着写。
    为什么你在群里提的技术问题没人回答?
  • 原文地址:https://www.cnblogs.com/hh13579/p/11674912.html
Copyright © 2011-2022 走看看