Description

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles: 
Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.

Input

The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1<=n<=100000. Then follow n integers h1,...,hn, where 0<=hi<=1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.

Output

For each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.

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(单调栈)的更多相关文章

  1. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  2. POJ 2559 Largest Rectangle in a Histogram(单调栈)

    传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  3. POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15831 ...

  4. PKU 2559 Largest Rectangle in a Histogram(单调栈)

    题目大意:原题链接 一排紧密相连的矩形,求能构成的最大矩形面积. 为了防止栈为空,所以提前加入元素(-1,0) #include<cstdio> #include<stack> ...

  5. [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)

    [POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...

  6. poj 2559 Largest Rectangle in a Histogram 栈

    // poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...

  7. stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram

    题目传送门 /* 题意:宽度为1,高度不等,求最大矩形面积 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极 ...

  8. poj 2559 Largest Rectangle in a Histogram (单调栈)

    http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 6 ...

  9. POJ2559 Largest Rectangle in a Histogram —— 单调栈

    题目链接:http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Lim ...

随机推荐

  1. Ansible 详细用法说明(二)

    setup:获取指定主机的facts. ===================================facts就是变量,内建变量 .每个主机的各种信息,cpu颗数.内存大小等.会存在fact ...

  2. SharePoint 2013 调查问卷的使用方法

    SharePoint 2013 调查问卷的使用方法 1,介绍调查问卷的用法. 2.图形和全部结果. 3,控制用户仅仅能看到自己答案. 1.确认有权限,假设没有管理管理权限请向管理员申请. 站点&quo ...

  3. CodeForces 404 Marathon ( 浮点数取模 -- 模拟 )

    B. Marathon time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  4. ubuntu下安装pycharm的方法

    linux下安装pycharm是比较麻烦的. 安装pycharm之前要安装好JDK8,依次执行如下的命令: sudo add-apt-repository ppa:webupd8team/java s ...

  5. Linux环境搭建:1. 安装VMware

    我家淘宝店,主要协助同学做毕业设计:https://shop104550034.taobao.com/?spm=2013.1.1000126.d21.pPCzDZ 1. 下载VMware 能够到我的3 ...

  6. strtok函数

    strtok函数是cstring文件里的函数 strtok函数是cstring文件里的函数 其功能是截断字符串 原型为:char *strtok(char s[],const char *delin) ...

  7. android 浮动窗体学习笔记及个人理解(仿360手机助手)

    很感谢原文作者 http://blog.csdn.net/guolin_blog/article/details/8689140 经自己理解 程序执行界面例如以下图: 1.程序入口界面 2.小浮动窗体 ...

  8. 【iOS系列】-iOS开发常用库文件总结

    这里是列举出得一部分,更多内容可参考https://github.com/darren90/Gather_iOS 码农周刊的总结 - 覆盖很广 调调的 - 很多开发相关内容都有体现 右滑返回的解决 - ...

  9. 【iOS系列】-autorelease的作用

    内存管理原则(配对原则):只要出现了new,alloc,retain方法,就要配对出现release,autorelease   1:对象存入到自动释放池中,当这个池子被销毁的时候他会对池子中所有的对 ...

  10. 小贝_mysql优化学习

    mysql优化 简要: 1.数据库设计优化 2.sql语句优化 3.表切割 4.读写分离技术 一.数据库设计优化 1.表设计要符合三范式.当然,有时也须要适当的逆范式 2.什么是三范式 一范式: 具有 ...