POJ 2559 Largest Rectangle in a Histogram(单调栈) && 单调栈
嗯...
题目链接:http://poj.org/problem?id=2559
一、单调栈:
1.性质:
单调栈是一种特殊的栈,特殊之处在于栈内的元素都保持一个单调性,可能为单调递增,也可能为单调递减。
2.模样:

这是一个单调递增的栈,如果我们插入的元素大于栈顶元素,则直接入栈;
如果我们插入的元素小于栈顶,则需要把栈内所有大于它的元素暂时出栈,将这个元素入栈后,再将暂时出栈的元素入栈,维护单调性。
二、模板:
这道题是单调栈的一道模板题:
先思考一个问题,如果题目中的矩形的高度都是单调递增的,如何得到最优解?
显然有一个贪心的策略,就是以每一个矩形的高度作为最终大矩形的高度,看最宽能是多少,然后统计最优解。
但如果进来的下一矩形比上一个低,它其实相当于限制了之前矩形的高度,那么之前矩形比这个矩形高出的高度在以后的统计中就没有丝毫用处了。
这样,我们实际上就得到了单调栈的模型,只需要维护一个单调栈,在维护单调性的弹出操作时统计宽度,更新答案即可在O(n)实际内得到最优解。
并且我们假设h[n+1]的位置有一个高度为0的矩形,最后将它加入单调栈时他会将所有矩形都弹出,那么答案也就完成最后的更新了。
AC代码:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std; int n, h[], w[], s[], top = ;
//h --> height w --> width s --> stack
inline bool input(){
scanf("%d", &n);
if(!n) return false;
for(int i = ; i <= n; i++)
scanf("%d", &h[i]);
h[n + ] = ;//最后用来清空栈
return true;
} inline long long work(){
long long ans = ;
for(int i = ; i <= n + ; i++){
if(h[i] > s[top]){
s[++top] = h[i];
w[top] = ;//宽度为1
}//满足单调
else{
int widthsum = ;//宽度和
while(s[top] > h[i]){
widthsum += w[top];
ans = max(ans, (long long) widthsum * s[top]);//注意高是s[top]
top--;
}
s[++top] = h[i];//此时s[top]已经小于h[i],满足单调
w[top] = widthsum + ;//合并
}
}
return ans;
} int main(){
while(input()){
printf("%lld\n", work());
top = ;
memset(s, , sizeof(s));
memset(h, , sizeof(h));
memset(w, , sizeof(w));
}
return ;
}
AC代码
POJ 2559 Largest Rectangle in a Histogram(单调栈) && 单调栈的更多相关文章
- [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)
[POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...
- stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram
题目传送门 /* 题意:宽度为1,高度不等,求最大矩形面积 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极 ...
- poj 2559 Largest Rectangle in a Histogram 栈
// poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...
- poj 2559 Largest Rectangle in a Histogram (单调栈)
http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 6 ...
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...
- POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15831 ...
- 题解报告:poj 2559 Largest Rectangle in a Histogram(单调栈)
Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...
- POJ 2559 Largest Rectangle in a Histogram -- 动态规划
题目地址:http://poj.org/problem?id=2559 Description A histogram is a polygon composed of a sequence of r ...
- POJ 2559 Largest Rectangle in a Histogram
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18942 Accepted: 6083 Description A hi ...
随机推荐
- tomcat8.5优化配置
参考文章: https://www.cnblogs.com/steven-snow/p/9262025.html 1.Tomcat内存使用调整 windows系统在bin/catalina.bat文件 ...
- 微信公众号获取access_token
一般我们在进行微信公众号开发的时候,都需要用到access_token,但是具体的获取及其使用方式如何呢?下面展示一种获取的方式(具体的微信公众号申请和配置,请参考开放文档,具体在这里就不详细说明了) ...
- 计算几何-多边形内核判定-HPI-poj3335
This article is made by Jason-Cow.Welcome to reprint.But please post the article's address. 先解决一个问题, ...
- 深度学习之tensorflow框架(中)
会话 开启会话 tf.Session用于完整的程序中 tf.InteractiveSession用于交互式上下文中的tensorflow 查看张量的值 都必须在会话里面 c_new_value=new ...
- Docker - 容器的 连接 与 退出
概述 连接容器, 退出容器 命令 run exec attach 退出 选项 -i -t -d 1. docker run 概述 docker run 通常用来创建新容器 docker run 的 三 ...
- bash_profile文件
bash_profile文件的作用 如何填写 如何生效
- Java Web实现使用浏览器从服务器下载文件(后台)
Java Web实现 使用浏览器从服务器下载文件. 下面实现两种情况的下载,需求如下: 需求(一):1.用户在页面填写表单. 2.填写完成后,选择下载,将表单内容发往后台. 3.后台根据内容生产一个文 ...
- CI 框架批量添加数据(如果数据库有就更新数据)
model: public function insert_select($values) { $sql = 'INSERT INTO ' . $this->_table_name . '(ar ...
- stopWatch 用法
package com.example.stopwatch; import org.springframework.util.StopWatch; public class TestStopWatch ...
- Scrapy 命令
Scrapy提供了两种类型的命令.一种必须在Scrapy项目中运行(针对项目(Project-specific)的命令),另外一种则不需要(全局命令).全局命令在项目中运行时的表现可能会与在非项目中运 ...