题解报告:poj 2559 Largest Rectangle in a Histogram(单调栈)
Description

Input
Output
Sample Input
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0
Sample Output
8
4000
解题思路:题意很清楚,就是找最大覆盖矩形的面积。这里要用到单调递增栈,相关讲解-->单调栈总结。其作用就是找到当前hi向左向右能延伸出最大长度的区间,即[L,R),最后最大的矩形面积就是max{(R[i]-L[i])*h[i]|0<=i<n}。时间复杂度是O(n)。
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stack>
using namespace std;
typedef long long LL;
const int maxn=1e5+;
int n,L[maxn],R[maxn];LL res,h[maxn];
stack<int> st;
int main(){
while(~scanf("%d",&n)&&n){
while(!st.empty())st.pop();memset(L,,sizeof(L));memset(R,,sizeof(R));
for(int i=;i<n;++i)scanf("%lld",&h[i]);
for(int i=;i<n;++i){
while(!st.empty()&&h[st.top()]>=h[i])st.pop();//找到i左边第一个比hi小的j右边一个点j+1,左闭
L[i]=st.empty()?:st.top()+;//如果栈空,说明hi不大于左边所有高度,那么区间左端点可延伸至0,这里为了方便计算区间长度
st.push(i);//再压入当前左端点值i
}
while(!st.empty())st.pop();res=;
for(int i=n-;i>=;--i){
while(!st.empty()&&h[st.top()]>=h[i])st.pop();//找到i右边第一个比hi小的j,右开
R[i]=st.empty()?n:st.top();//如果栈空,说明hi不大于右边所有高度,那么区间右端点可延伸至n,同样为了方便计算区间长度
st.push(i);//再压入当前右端点值i
}
for(int i=;i<n;++i)//找最大面积
res=max(res,h[i]*(R[i]-L[i]));
cout<<res<<endl;
}
return ;
}
题解报告:poj 2559 Largest Rectangle in a Histogram(单调栈)的更多相关文章
- 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 ...
- PKU 2559 Largest Rectangle in a Histogram(单调栈)
题目大意:原题链接 一排紧密相连的矩形,求能构成的最大矩形面积. 为了防止栈为空,所以提前加入元素(-1,0) #include<cstdio> #include<stack> ...
- [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)
[POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...
- poj 2559 Largest Rectangle in a Histogram 栈
// poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...
- 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 (单调栈)
http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 6 ...
- POJ2559 Largest Rectangle in a Histogram —— 单调栈
题目链接:http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Lim ...
随机推荐
- 书评第003篇:《0day安全:软件漏洞分析技术(第2版)》
本书基本信息 丛书名:安全技术大系 作者:王清(主编),张东辉.周浩.王继刚.赵双(编著) 出版社:电子工业出版社 出版时间:2011-6-1 ISBN:9787121133961 版次:1 页数:7 ...
- properties文件读取配置信息
public static void main(String[] args){ String printerName=""; String path = "C:\\Bar ...
- 针对OpenSSL吐嘈的吐嘈-如此唱反调
前些天写了一篇<令人作呕的OpenSSL>,顿时引来了大量的恶评.令我非常尴尬,同一时候也认为悲哀. 假设说you can you up之类的,我认为起码这人看出了我的本意,仅仅是怀疑我的 ...
- 在做java 的web开发,为什么要使用框架
现在做项目都会使用框架,现在很常见的框架就是SSH(Struts+SpringMVC+spring+hibernate),SSM(Struts/springMVC+Spring+Hibernate), ...
- iOS设备,fixed布局出问题
window.deviceId = '{{$deviceId}}'; window.iOS = navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? t ...
- YTU 2402: Common Subsequence
2402: Common Subsequence 时间限制: 1 Sec 内存限制: 32 MB 提交: 63 解决: 33 题目描述 A subsequence of a given seque ...
- div+css布局教程系列1
<!doctype html><html><head><meta charset="utf-8"><title>简单布局 ...
- 使用JSTL 对在页面上对 0,0,1 的分割处理 forTokens
使用JSTL 对在页面上对 0,0,1 的分割处理 <tr onmouseover="currentcolor=this.style.backgroundColor;this.styl ...
- I.MX6 Android shutdown shell command
/******************************************************************************* * I.MX6 Android shu ...
- 牛客练习赛13D:幸运数字Ⅳ(康托展开) F:关键字排序
链接:https://www.nowcoder.com/acm/contest/70/D 题目: 定义一个数字为幸运数字当且仅当它的所有数位都是4或者7. 比如说,47.744.4都是幸运数字而5.1 ...