题目链接

传送门

题意

求在\(n\times m\)的\(01\)子矩阵中找出面积第二大的内部全是\(1\)的子矩阵的面积大小。

思路

处理出每个位置往左连续有多少个\(1\),然后对每一列跑单调栈,记得处理由同一矩阵贡献的面积。

代码实现如下

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 1e5 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; stack<int> st;
int n, m, mx, mn;
int mp[1007][1007], vis[1007][1007];
int num[1007][1007], ls[1007], rs[1007]; int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
scanf("%1d", &mp[i][j]);
}
}
for(int i = 1; i <= n; ++i) {
int L = 0;
for(int j = 1; j <= m; ++j) {
if(mp[i][j] == 1) {
++L;
} else {
L = 0;
}
num[i][j] = L;
}
}
for(int j = 1; j <= m; ++j) {
while(st.size()) st.pop();
for(int i = 1; i <= n; ++i) {
while(st.size() && num[st.top()][j] > num[i][j]) {
rs[st.top()] = i - 1;
st.pop();
}
st.push(i);
}
while(st.size()) {
rs[st.top()] = n;
st.pop();
}
while(st.size()) st.pop();
for(int i = n; i >= 1; --i) {
while(st.size() && num[st.top()][j] > num[i][j]) {
ls[st.top()] = i + 1;
st.pop();
}
st.push(i);
}
while(st.size()) {
ls[st.top()] = 1;
st.pop();
}
for(int i = 1; i <= n; ++i) {
if(vis[ls[i]][rs[i]]) continue;
vis[ls[i]][rs[i]] = 1;
int ans = num[i][j] * (rs[i] - ls[i] + 1);
if(ans > mx) {
mn = mx;
mx = ans;
} else {
mn = max(mn, ans);
}
ans = num[i][j] * (rs[i] - ls[i]);
if(ans > mx) {
mn = mx;
mx = ans;
} else {
mn = max(mn, ans);
}
}
for(int i = 1; i <= n; ++i) vis[ls[i]][rs[i]] = 0;
}
printf("%d\n", mn);
return 0;
}

2019年牛客多校第二场 H题Second Large Rectangle的更多相关文章

  1. 2019牛客多校第二场H题(悬线法)

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

  2. MAZE(2019年牛客多校第二场E题+线段树+矩阵乘法)

    题目链接 传送门 题意 在一张\(n\times m\)的矩阵里面,你每次可以往左右和下三个方向移动(不能回到上一次所在的格子),\(1\)表示这个位置是墙,\(0\)为空地. 现在有\(q\)次操作 ...

  3. Kth Minimum Clique(2019年牛客多校第二场D题+k小团+bitset)

    目录 题目链接 题意 思路 代码 题目链接 传送门 题意 找第\(k\)小团. 思路 用\(bitset\)来标记每个结点与哪些结点直接有边,然后进行\(bfs\),在判断新加入的点与现在有的点是否都 ...

  4. 2019年牛客多校第二场 F题Partition problem 爆搜

    题目链接 传送门 题意 总共有\(2n\)个人,任意两个人之间会有一个竞争值\(w_{ij}\),现在要你将其平分成两堆,使得\(\sum\limits_{i=1,i\in\mathbb{A}}^{n ...

  5. Cutting Bamboos(2019年牛客多校第九场H题+二分+主席树)

    题目链接 传送门 题意 有\(n\)棵竹子,然后有\(q\)次操作,每次操作给你\(l,r,x,y\),表示对\([l,r]\)区间的竹子砍\(y\)次,每次砍伐的长度和相等(自己定砍伐的高度\(le ...

  6. 2019年牛客多校第一场 H题XOR 线性基

    题目链接 传送门 题意 求\(n\)个数中子集内所有数异或为\(0\)的子集大小之和. 思路 对于子集大小我们不好维护,因此我们可以转换思路变成求每个数的贡献. 首先我们将所有数的线性基的基底\(b\ ...

  7. 2019年牛客多校第一场B题Integration 数学

    2019年牛客多校第一场B题 Integration 题意 给出一个公式,求值 思路 明显的化简公式题,公式是分母连乘形式,这个时候要想到拆分,那如何拆分母呢,自然是裂项,此时有很多项裂项,我们不妨从 ...

  8. 牛客多校第二场H Second Large Rectangle 单调栈or悬线法

    Second Large Rectangle 题意 给出n*m的01矩阵,问由1组成的第二大的矩阵的大小是多少? 分析 单调栈(or 悬线法)入门题 单调栈 预处理出每一个点的最大高度,然后单调栈每一 ...

  9. 2019 牛客多校第二场 H Second Large Rectangle

    题目链接:https://ac.nowcoder.com/acm/contest/882/H 题目大意 给定一个 n * m 的 01 矩阵,求其中第二大的子矩阵,子矩阵元素必须全部为 1.输出其大小 ...

随机推荐

  1. 缓解DDoS && cc 的最佳Linux内核设置 (转)

    https://javapipe.com/blog/iptables-ddos-protection/ kernel.printk = 4 4 1 7 kernel.panic = 10 kernel ...

  2. replace into 详解 update mysql

    转replace 与 update 区分本文主要对比一下 Sqlite 中的 replace 语句和 update 语句 . 在本例中使用如下数据库表:   图1 该表的表名为student, 存储学 ...

  3. scala 特质的应用

    一.为类提供可以堆叠的改变 package com.jason.qianfeng trait Loggertest { def logger(msg: String) } trait ConsoleL ...

  4. 【转帖】Kafka入门介绍

    Kafka入门介绍 https://www.cnblogs.com/swordfall/p/8251700.html 最近在看hdoop的hdfs 以及看了下kafka的底层存储,发现分布式的技术基本 ...

  5. Java中是使用增强for的null问题

    在使用List和Map等集合时,我们经常会使用增强for来进行遍历.但是这里面会存在一些问题.比如当你进行数据库查询是,得到的返回结果是List集合时,如果没有查询到符合要求的数据时List集合时nu ...

  6. 【LEETCODE】61、对leetcode的想法&数组分类,适中级别,题目:162、73

    这几天一直再想这样刷题真的有必要么,这种单纯的刷题刷得到尽头么??? 这种出题的的题目是无限的随便百度,要多少题有多少题,那么我这一直刷的意义在哪里??? 最近一直苦苦思考,不明所以,刷题刷得更多的感 ...

  7. C# 历遍对象属性

    今天有个网友问如何历遍对象的所有公共属性,并且生成XML.采用序列化方式的话比较简单,我写个手工解析的例子,这样能让初学者更加理解也比较灵活,记录一下吧或许会有人用到. 对象模型: public cl ...

  8. JUC-FutureTask

    得到别的线程任务的返回值 import lombok.extern.slf4j.Slf4j; import java.util.concurrent.Callable; import java.uti ...

  9. 倾斜动画(SkewTransform)

    Silverlight中的倾斜变化动画(SkewTransform)能够实现对象元素的水平.垂直方向的倾斜变化动画效果.我们现实生活中的倾斜变化效果是非常常见的,比如翻书的纸张效果,关门开门的时候门缝 ...

  10. java之spring之scope和autowiring

    目录结构如下: UserDao.java package cn.sxt.dao; public interface UserDao { public void add(); } UserDaoImpl ...