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],求最少需要几个矩形才能覆盖这个图形. ...
随机推荐
- 一个上传图片,预览图片的小demo
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- [RTMP] 国内各大视频直播CDN厂商推流抢流行为分析
背景 当存在一个推流客户端正在向rtmp://xxx.com/live/yyy推流时,又有另外一个推流客户端同时对这个地址进行推流,会发生什么呢? 查阅了 Adobe RTMP Spec 发现规范本身 ...
- 【MySQL】使用WHERE子句 - 过滤数据
第6章 过滤数据 文章目录 第6章 过滤数据 1.使用WHERE子句 2.WHERE子句操作符 2.1.检查单个值 2.2.不匹配检查 2.3.范围值检查 2.4.空值检查 3.小结 简单记录 - M ...
- show slave status常用参数备忘
mysql> show slave status\G*************************** 1. row *************************** Slave_IO ...
- C/C++内存对齐详解
1.什么是内存对齐 还是用一个例子带出这个问题,看下面的小程序,理论上,32位系统下,int占4byte,char占一个byte,那么将它们放到一个结构体中应该占4+1=5byte:但是实际上,通过运 ...
- Py编程方法,尾递归优化,map函数,filter函数,reduce函数
函数式编程 1.面向过程 把大的问题分解成流程,按照流程来编写过程 2.面向函数 面向函数编程=编程语言定义的函数+数学意义上的函数先弄出数学意义上的方程式,再用编程方法编写这个数学方程式注意面向函数 ...
- 十九、更改LCD显示屏
一.裸机修改 之前测试用的屏幕是480*272的分辨率,现在要换成800*480的屏,因此要对软件代码进行修改. 对于裸机驱动而言,主要有两个点需要注意,一个是屏幕分辨率变了,因此初始化的时候与屏幕分 ...
- API服务接口签名代码与设计,如果你的接口不走SSL的话?
在看下面文章之前,我们先问几个问题 rest 服务为什么需要签名? 签名的几种方式? 我认为的比较方便的快捷的签名方式(如果有大神持不同意见,可以交流!)? 怎么实现验签过程 ? 开放式open ap ...
- CentOS 镜像下载地址
CentOS镜像地址:http://isoredirect.centos.org/altarch/7/isos/i386/
- Building a Robust Live Reloader with WebSockets and Go — Brandur Leach https://brandur.org/live-reload
Building a Robust Live Reloader with WebSockets and Go - Brandur Leach https://brandur.org/live-relo ...