zjnu1735BOB (单调队列,单调栈)
Description
Little Bob is a famous builder. He bought land and wants to build a house. Unfortunately, the problem is the
land’s terrain, it has a variable elevation.
The land is shaped like a rectangle, N meters wide and M meters long. It can be divided into N*M squares
(see the image). Bob’s house will be shaped like a rectangle that has sides parallel with the land’s edges and
its vertices coincide with the vertices of the squares. All the land covered by Bob’s house must be of equal
elevation to prevent it from collapsing.

Calculate the number of ways Bob can build his house!
Input
The first line of input contains integers N and M (1 <= N, M <= 1000).
Each of the following N lines contains M integers aij (1 <= aij <= 1000000000), respectively the height of each square of
land.
Warning: Please use faster input methods beacuse the amount of input is very large. (For example, use scanf
instead of cin in C++ or BufferedReader instead of Scanner in Java.)
Output
The first and only line of output must contain the required number from the task statement.
Sample Input
2 2 2
2 2 1
1 1 1
2 1 2
1 2 1
Sample Output
Hint
Clarification of the first example: Some of the possible house locations are rectangles with opposite vertices in (0,0)-
(1,1), (0,0)-(0,2) (height 2) i (2,0)-(2,2), (1,2)-(2,2) (height 1). The first number in the brackets represents the row number
and the second one the column number (0-indexed).
题意:给你一个n*m的矩阵,每一个格子都有自己的价值,让你在矩阵中找一些格子,使得矩形里的格子的价值都相同,问最多能找到的矩形数。
思路:如果是普通暴力的话,那么时间复杂度为O(n*m*m)爆了,所以要找其他方法。我们可以用单调队列做(单调栈也可以做的,其实它们两者的本质是一样的),这题所要维护的东西和之前求一个平面内的最大矩形面积不同,这里求的是最多的矩形个数,我们可以先把二维矩阵转化为一维,即对于每一行的格子,把上面和它价值相同的格子数作为它的高度。那么这个问题就转化成了有许多长方形格子放在地上,让你找到包含矩形底的矩形总个数。用单调队列的时候要维护三个值,q[][0]表示高度,q[][1]表示以它为右下角格子的矩形个数,q[][2]表示坐标,维护一个严格递增单调队列就行了。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 1005
int gra[maxn][maxn],a[maxn][maxn];
int q[1111111][3];
int main()
{
int n,m,i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
scanf("%d",&gra[i][j]);
a[i][j]=1;
if(i>1 && gra[i][j]==gra[i-1][j]){
a[i][j]+=a[i-1][j];
}
}
}
ll cnt=0;
int front,rear,qidian;
for(i=1;i<=n;i++){
gra[i][0]=-1;
for(j=1;j<=m;j++){
if(gra[i][j]==gra[i][j-1]){
while(front<=rear && q[rear][0]>=a[i][j]){
rear--;
}
rear++;
q[rear][0]=a[i][j];q[rear][2]=j;
if(rear==1){
q[rear][1]=a[i][j]*(j-qidian); //这里相当于底*高
}
else{
q[rear][1]=a[i][j]*(j-q[rear-1][2])+q[rear-1][1] ; //这里要加上队列里前一个高度比它低的矩形算出来的符合要求的矩形
}
cnt+=q[rear][1];
}
else{
front=1;rear=1;
qidian=j-1;
q[rear][0]=a[i][j];
q[rear][1]=a[i][j];
q[rear][2]=j;
cnt+=a[i][j];
}
}
}
printf("%lld\n",cnt);
}
return 0;
}
zjnu1735BOB (单调队列,单调栈)的更多相关文章
- 单调队列 && 单调栈
单调队列 && 单调栈 单调队列 维护某个滑动区间的min or max,可用于dp的优化 以维护min为例,采用STL双端队列实现 每次加入元素x前 先检查队首元素==滑动后要删除的 ...
- 联赛模拟测试18 A. 施工 单调队列(栈)优化DP
题目描述 分析 对于 \(Subtask\ 1\),可以写一个 \(n^3\) 的 \(DP\),\(f[i][j]\) 代表第 \(i\) 个建筑高度为 \(j\) 时的最小花费,随便转移即可 时间 ...
- 数据结构录 之 单调队列&单调栈。
队列和栈是很常见的应用,大部分算法中都能见到他们的影子. 而单纯的队列和栈经常不能满足需求,所以需要一些很神奇的队列和栈的扩展. 其中最出名的应该是优先队列吧我觉得,然后还有两种比较小众的扩展就是单调 ...
- 单调队列&单调栈
单调队列 例题: Poj 2823给定一个数列,从左至右输出每个长度为m的数列段内的最小数和最大数.数列长度:N<=106,m<=N 对于单调队列,我们这样子来定义: 1.维护区间最值 2 ...
- 数据结构录 之 单调队列&单调栈。(转)
http://www.cnblogs.com/whywhy/p/5066306.html 队列和栈是很常见的应用,大部分算法中都能见到他们的影子. 而单纯的队列和栈经常不能满足需求,所以需要一些很神奇 ...
- 大视野 1012: [JSOI2008]最大数maxnumber(线段树/ 树状数组/ 单调队列/ 单调栈/ rmq)
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 9851 Solved: 4318[Submi ...
- 小Z爱序列(NOIP信(sang)心(bin)赛)From FallDream(粗制单调队列&单调栈的算法解析)
原题: 小Z最擅长解决序列问题啦,什么最长公共上升然后下降然后上升的子序列,小Z都是轻松解决的呢. 但是小Z不擅长出序列问题啊,所以它给了你一道签到题. 给定一个n个数的序列ai,你要求出满足下述条件 ...
- POJ 3494 Largest Submatrix of All 1’s 单调队列||单调栈
POJ 3494 Largest Submatrix of All 1’s Description Given a m-by-n (0,1)-matrix, of all its submatrice ...
- 单调队列&单调栈归纳
单调队列 求长度为M的区间内的最大(小)值 单调队列的基本操作,也就是经典的滑动窗口问题. 求长度为M的区间内最大值和最小值的最大差值 两个单调队列,求出长度为M的区间最大最小值的数组,分别求最大最小 ...
- 【NOIP数据结构专项】单调队列单调栈
[FZYZ P1280 ][NOIP福建夏令营]矩形覆盖 Description 有N个矩形,矩形的底边边长为1,且均在X轴上,高度给出,第i个矩形的高为h[i],求最少需要几个矩形才能覆盖这个图形. ...
随机推荐
- 图片质量评估论文 | 无监督SER-FIQ | CVPR2020
文章转自:同作者微信公主号[机器学习炼丹术].欢迎交流,共同进步. 论文名称:SER-FIQ: Unsupervised Estimation of Face Image Quality Based ...
- LeetCode106 从中序和后序序列构造二叉树
题目描述: 根据一棵树的中序遍历与后序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [ ...
- show engine innodb status
TRANSACTIONS------------Trx id counter 2003909(当前事务号)Purge done for trx's n:o < 2003905 (清理线程完成到了 ...
- 【Linux】在docker上部署grafana+zabbix监控实录
------------------------------------------------------------------------------------------------- ...
- leetcode 321. 拼接最大数(单调栈,分治,贪心)
题目链接 https://leetcode-cn.com/problems/create-maximum-number/ 思路: 心都写碎了.... 也许就是不适合吧.... 你是个好人... cla ...
- XEE - Pikachu
概述 XXE -"xml external entity injection"既"xml外部实体注入漏洞".概括一下就是"攻击者通过向服务器注入指定的 ...
- online创建索引中途取消导致索引无法删除解决办法
问题:有一个表ID栏位没有索引,但是在一个update语句的where中被使用,因此打算online创建索引,但是长时间没有成功,此时决定取消,取消后发现索引无法删除 过程: 数据库监控报警有行锁,进 ...
- k8s之共享存储概述以及演示
共享存储机制 k8s对有状态的容器应用或者需要对数据进行持久化的应用,在之前的篇章说过,可以将容器内的目录挂载到宿主机的容器目录或者emptyDir临时存储卷. 另外,k8s还开放了两个资源,分别是P ...
- cfsetispeed、cfsetospeed和cfsetspeed探究
在我https://www.cnblogs.com/Suzkfly/p/11055532.html这篇博客中有一个疑问,就是在串口设置波特率的域中,没有将输入输出波特率分开,那为什么会有几个不同的设置 ...
- Spring之 IOC&依赖注入
0x01.Spring 1什么是Spring Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的(解耦). 框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组 ...