zoukankan      html  css  js  c++  java
  • 2019 牛客暑期多校 第二场 H Second Large Rectangle (单调栈)

    题目:https://ac.nowcoder.com/acm/contest/882/H

    题意:一个大的01矩阵,然后现在要求第二大的全一矩阵是多少

    思路:在这里我们首先学习一下另一个东西,怎么求直方图的最大面积

    这个大佬写的博客非常好    https://www.cnblogs.com/linkstar/p/6139668.html   然后知道怎么求直方图面积后,我们可以先预处理,我们预处理出上到下的连续,举个栗子

    1010   1010

    1110    ->    2120

    1111    3231

    这样做有什么好处呢,这样我们就能O(1)求出上面到当前连续最长多少。然后我们现在只要枚举我的下边界是谁,然后每个下边界求一遍最大面积即可,这里要注意的是第二大有可能是最大少一行或者少一列,然后取最大值即可

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<stack>
    using namespace std;
    const int maxn=1080;
    stack<int>r;
    int n,m,h[maxn],a[maxn],mx1,mx2;
    char str[maxn];
    void solve(int x)
    {
        if(x>mx1)
            mx2=mx1,mx1=x;
        else if(x>mx2) mx2=x;
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;++i)
        {
                cin>>str+1;
            for(int j=1;j<=m;++j)
            {
                if(str[j]-'0') a[j]+=1;
                else a[j]=0;
                //h[j]=a[j];
            }
            r.push(0);
            for(int j=1;j<=m+1;++j)
            {
                while(a[j]<a[r.top()])
                {
                    int index = r.top();
                    r.pop();
                    int x=j-1-r.top();
                    int y=a[index];
                    solve(x*y);
                    solve((x-1)*y);
                    solve(x*(y-1));
                }
                r.push(j);
     
            }
        }
        printf("%d
    ",mx2);
    }
    View Code
  • 相关阅读:
    在C#中使用正则表达式
    C++流操纵算子(格式控制)
    linux(Fedora) doxygen 的安装和使用
    UTF8
    java cookie全解析
    Fedora16 安装Adobe Flash Player方法
    工程素养
    感悟数据封装
    谷歌如何保护隐私
    openCV画的词法分析图
  • 原文地址:https://www.cnblogs.com/Lis-/p/11333233.html
Copyright © 2011-2022 走看看