题目传送门//res tp nowcoder

目的

给定n*m 01矩阵,求矩阵内第二大矩形

分析

O(nm)预处理01矩阵为n个直方图,问题转换为求n个直方图中的第二大矩形。单调栈计算,同时维护前二大的面积即可。

对于X*Y的矩阵,我们只需考虑X * Y,X * (Y-1),(X-1) * Y即可

#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
typedef long long ll;
const int L = 1010;
ll max1,max2; inline void Update(ll x,ll y){
ll t = x*y;
if(t<max2)return;
else if(t<max1) max2 = t;
else{
max2 = max1;max1 = t;
}
}
inline void update(ll x,ll y){
Update(x,y);
Update(x,y-1);
Update(x-1,y);
} void Maxrectangle(ll H[],int n){
stack<int>M;
for(int k = 1;k<=n;){
if(M.empty()||H[M.top()] <=H[k])
M.push(k++);
else{
ll h = H[M.top()];M.pop();
if(M.empty())
update((k-1),h);
else
update((k-M.top()-1),h);
}
}
while(!M.empty()){
ll h = H[M.top()];M.pop();
if(M.empty())
update(n,h);
else
update((n-M.top()),h);
}
} int n,m;
char s[L][L];
ll num[L][L];
int main(){
cin>>n>>m;
for(int i = 1;i<=n;++i) cin>>s[i];
for(int j = 1;j<=m;++j)for(int i = 1;i<=n;++i){
if(s[i][j-1] == '0') num[i][j] = 0;
else if(s[i][j-1] == '1') num[i][j] = num[i-1][j] + 1;
else num[i][j] = 0;
}
for(int i = 1;i<=n;++i) Maxrectangle(num[i],m);
cout<<max2<<endl; }

19牛客暑期多校 round2 H 01矩阵内第二大矩形的更多相关文章

  1. 19牛客暑期多校 round2 F dfs

    题目传送门//res tp nowcoder dfs 先将所有人都归于一队,之后从一队中取出人放置到另一个队. #include<iostream> #include<cstdio& ...

  2. 19牛客暑期多校 round1 A 有关笛卡尔树的结论

    题目传送门//res tp nowcoder 分析 定理:B1~B2当且仅当B1与B2有同构的笛卡尔树. (B₁~B₂ iff B₁ and B₂ have isomorphic Cartesian ...

  3. 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题)

    layout: post title: 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题) author: "luowentaoaa" c ...

  4. 2021牛客暑期多校训练营3 J 思维

    传送门 J-Counting Triangles_2021牛客暑期多校训练营3 (nowcoder.com) 题目 Goodeat finds an undirected complete graph ...

  5. 2019 牛客暑期多校 第二场 H Second Large Rectangle (单调栈)

    题目:https://ac.nowcoder.com/acm/contest/882/H 题意:一个大的01矩阵,然后现在要求第二大的全一矩阵是多少 思路:在这里我们首先学习一下另一个东西,怎么求直方 ...

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

    示例一: 输入  : 1 2 01 输出: 0 示例二: 输入  : 1 3 101 输出: 1 示例三(自己自测找错误用的): 输入  : 6 610011111101111111111111111 ...

  7. 2019牛客暑期多校训练营(第七场)E F H I

    E Find the median 题意:每次往序列中增加连续的[l,r]的数,每加入一次就询问当前序列的中位数. 解法:此题没有要求在线,那么直接离线+线段树+二分就可以了.求出每个端点之后排序得到 ...

  8. B-xor_2019牛客暑期多校训练营(第四场)

    题意 给出n个数组(每组数个数不定),m个询问 l, r, x 序号在区间\([l,r]\)的每个数组是否都可以取出任意个数异或出x 题解 判断一个数组能否异或出x,是简单的线性基问题 判断多个线性基 ...

  9. 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)

    题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9:  对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可.     后者mod=1e9,5才 ...

随机推荐

  1. Try-Catch-Finally代码块中的return

    测试类的原型是这样子的 public class TryCatchFinallyToReturn { public static void main(String[] args) { System.o ...

  2. RuntimeException和Exception的区别

    1.java将所有的错误封装为一个对象,其根本父类为Throwable, Throwable有两个子类:Error和Exception. 2.Error是Throwable 的子类,用于指示合理的应用 ...

  3. 算法-java实现

    1. 质因数分解 public static List<Integer> factorize(int n){ List<Integer> factors = new Array ...

  4. python+socket+jq实现web页面实时输出结果

    例如有这样一个需求: 在终端上进行ping操作,现在想把这个这个操作放到web页面上进行,并且实现实时输出的效果. 来分析下具体实现过程 第一步,传统的http请求实现这个有点不太友好,因为这里边是一 ...

  5. uiautomator2 wifi连接手机

    [实施方法] 手机和电脑同时连接到同一个wifi上 1.开启远程adb #开启远端adb,这一步需要手机通过USB连接到电脑 adb tcpip 5555 #结果如下:restarting in TC ...

  6. ubantu 安装boost库 c++connector

    安装libmysqlcppconn: sudo apt-get install libmysqlcppconn-dev 安装libboost: sudo apt-get install libboos ...

  7. nginx 记录

    正则 ~ 区分大小写匹配 ~* 不区分大小写匹配 !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 ^ 以什么开头的匹配 $ 以什么结尾的匹配 转义字符\ 可以转. * ?等 * 代表任意字符 ...

  8. 【English】What is a Java StringWriter, and when should I use it?(转帖)

    转帖地址:http://www.it1352.com/989366.html Question: What is a Java StringWriter, and when should I use ...

  9. kotlin使用中辍标记法调用函数

    fun main(arg: Array<String>) { var str = "hello world" print(str div("l")) ...

  10. Build Telemetry for Distributed Services之OpenTracing项目

    中文文档地址:https://wu-sheng.gitbooks.io/opentracing-io/content/pages/quick-start.html 中文github地址:https:/ ...