链接:https://ac.nowcoder.com/acm/contest/888/A

题目描述
Gromah and LZR entered the great tomb, the first thing they see is a matrix of size n×mn\times mn×m, and the elements in the matrix are all 00_{}0​ or 11_{}1​.

LZR finds a note board saying “An all-one matrix is defined as the matrix whose elements are all 11_{}1​, you should determine the number of all-one submatrices of the given matrix that are not completely included by any other all-one submatrices”.

Meanwhile, Gromah also finds a password lock, obviously the password should be the number mentioned in the note board!

Please help them determine the password and enter the next level.

输入描述:
The first line contains two positive integers n,mn,m_{}n,m​, denoting the size of given matrix.

Following nn_{}n​ lines each contains a string with length mm_{}m​, whose elements are all 00_{}0​ or 11_{}1​, denoting the given matrix.

1≤n,m≤30001\le n,m \le 30001≤n,m≤3000

输出描述:
Print a non-negative integer, denoting the answer.
输入
3 4
0111
1110
0101
输出
5
说明
The 5 matrices are (1,2)−(1,4),  (1,2)−(2,3),  (1,2)−(3,2),  (2,1)−(2,3),  (3,4)−(3,4)(1,2)-(1,4), ; (1,2)-(2,3), ; (1,2)-(3,2), ; (2,1)-(2,3), ; (3,4)-(3,4)_{}(1,2)−(1,4),(1,2)−(2,3),(1,2)−(3,2),(2,1)−(2,3),(3,4)−(3,4)​.
赛后补题,赛场上一点思路没有。。
poj上有一个最大全一矩阵的题目,也是单调栈的题目。但是没想起来怎么用单调栈解决这个问题。对于每一个num[i][j]我们记录它向上的连续一的个数,然后枚举每一行,从前向后枚举列。单调找维护两个东西,就是向上的最长长度和矩阵的最左位置pos。那么我们把最上最左可到达的距离记录下来了,不能向右扩,那么只看能不能向下扩就行了。如果不能的话,就到头了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=3e3+100;
int a[maxx][maxx];
int num[maxx][maxx];
stack< pair<int,int> > s;
int n,m;int main()
{scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){for(int j=1;j<=m;j++)scanf("%1d",&a[i][j]);}for(int i=1;i<=n;i++){for(int j=1;j<=m;j++)num[i][j]=a[i][j]?num[i-1][j]+a[i][j]:0;//记录每一列的连续1的前缀和 }int ans=0;for(int i=1;i<=n;i++){int tmp=-1;while(!s.empty()) s.pop();for(int j=1;j<=m+1;j++){int pos=j;while(!s.empty()&&s.top().first>num[i][j]){if(s.top().second<=tmp)  ans++;//这个矩阵最左边小于当前不可扩的最右边,就说明这个矩阵不可扩 pos=s.top().second;//更新pos就是下个矩阵的最左边。 s.pop();}if(!num[i+1][j]) tmp=j;//下面是0就不可下扩,那样就把最右边的记录下来 if(num[i][j]&&(s.empty()||s.top().first<num[i][j])) s.push(make_pair(num[i][j],pos));//入栈的是矩阵的最左边(i+num-1,j)->(i,j)就是一个矩阵 }}printf("%d\n",ans);
}

努力加油a啊,(o)/~

2019牛客第八场A All-one Matrices(单调栈)相关推荐

  1. CDMA(牛客第八场构造题)

    链接:https://ac.nowcoder.com/acm/contest/888/C 来源:牛客网 Gromah and LZR have entered the third level. The ...

  2. Gemstones(牛客第八场多校)

    链接:https://ac.nowcoder.com/acm/contest/888/G 来源:牛客网 Gromah and LZR have entered the seventh level. T ...

  3. 牛客第八场_E-Enigmatic Partition(神仙差分)

    文章目录 [E-Enigmatic Partition](https://ac.nowcoder.com/acm/contest/5673/E) 题意 收获 代码 E-Enigmatic Partit ...

  4. 2019牛客第四场I题 string

    链接:https://ac.nowcoder.com/acm/contest/884/I 题意:求string串有多少个本质不同的子串,且这些子串之间两两不存在 a==rev(a),即不存在长度1以上 ...

  5. 2019牛客暑期多校训练营(第五场)C generator 2 (BSGS)

    2019牛客暑期多校训练营(第五场)C generator 2 思路 x0=x0x_0 = x_0x0​=x0​ x1=a∗x0∗bx_1 = a * x_0 * bx1​=a∗x0​∗b x2=a∗ ...

  6. 2019牛客多校第一场

    2019牛客多校第一场 题号 题目 知识点 A Monotonic Matrix B Symmetric Matrix C Fluorescent 2 D Two Graphs E Removal F ...

  7. 2019牛客多校训练第十场F Popping Balloons

    2019牛客多校训练第十场F Popping Balloons 题意:二维平面内给你若干个点,然后你可以在x轴和y轴分别射三枪(每一枪的间隔是R),问最多能射掉多少气球. 题解:贪心.这个应该只能算作 ...

  8. 2019牛客多校第九场AThe power of Fibonacci(广义BM)

    2019牛客多校第九场AThe power of Fibonacci(广义BM) 题目大意 求斐波那契数列m次方的前n项和 解题思路 显然,斐波那契的m次方前缀和依然是线性递推,因此考虑用exBM求解 ...

  9. 2019牛客暑期多校训练营(第三场)H.Magic Line

    2019牛客暑期多校训练营(第三场)H.Magic Line 题目链接 题目描述 There are always some problems that seem simple but is diff ...

最新文章

  1. Vue.js 是什么
  2. hdu5012 水搜索
  3. SAP成都研究院DevOps那些事
  4. 【转载保存】Lucene 实战教程第六章 Lucene 的精确、包含、集合查询 Query 的简单使用
  5. 精仿B站源码+自动采集360视频
  6. lvs的dr和nat模式配置备忘
  7. C#调用C++动态库时类型转换
  8. macOS Monterey 12.0beta4黑苹果镜像虚拟机版本
  9. LoadRunner压力测试:详细操作流程
  10. 2012 r2 万能网卡驱动_无线网卡怎么用,我来教您无线网卡怎么用的方法
  11. java实现ssdp协议_SSDP协议最简单的Reactor / Netty实现是什么?
  12. 国稻种芯百团计划行动 丰收节贸促会·黎志康:惠及亚非18国家
  13. idea--Preferences
  14. CF 339C Xenia and Weights 背包
  15. 新中新 金龙一卡通 TPE_StartTPE 加载授权文件失败 JAVA
  16. SDNUOJ 1168.FBI树
  17. 基本流程图的制作-通过在线制图工具绘制
  18. Dapper - 微型 ORM 之王 (C#.NET)
  19. struts2 漏洞测试工具
  20. 精品基于Uniapp+SSM实现的Android的校园新闻管理系统实现的App

热门文章

  1. IOS15仿微信我的页面
  2. android悬浮窗代码布局,三行代码实现Android应用内悬浮窗,无需一切权限,适配所有ROM和厂商...
  3. 大数据翻页_【干货】大数据翻页的难点和技巧
  4. 查询成绩最好的前两名_一级建造师成绩查询前这几件事很重要
  5. 二叉树后序遍历_LeetCode算法145. 二叉树的后序遍历
  6. canvas html 动态,canvas.html
  7. django不修改数据库创外键_Django——model(建表,增删改查,外键,多对多)
  8. python开发sqlite3完整_让Python更加充分的使用Sqlite3-阿里云开发者社区
  9. 程序一直吐司提示获取缓存记录失败以及吐司显示set Alias success
  10. python写日志文件_Python logging日志模块 配置文件方式