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
题意图给的很明显了,手写栈代码如下
#include<iostream>
#include<cstring>
using namespace std;
int a[],s[],w[];
int main()
{
int n,x;
long long ans;
while()
{
int p=;
ans=;
memset(a,,sizeof(a));
memset(w,,sizeof(a));
memset(s,,sizeof(s));
cin>>n;
if(n==)
break;
for(int i=;i<=n;i++)
cin>>a[i];
a[n+]=p;
for(int i=;i<=n+;i++)
{
if(a[i]>s[p])
{
s[++p]=a[i];
w[p]=;
}
else
{
int width=;
while(s[p]>a[i])
{
width=width+w[p];
ans=max(ans,(long long )width*s[p]);
p--;
}
s[++p]=a[i],w[p]=width+;
}
}
cout<<ans<<endl;
}
}

STL代码

#include<iostream>
#include<cstring>
#include<stack>
using namespace std; int main()
{
int n,x;
long long ans;
while()
{
int p=;
ans=;
cin>>n;
if(n==)
break;
stack< pair <int ,int > >s;
s.push(make_pair(,));
for(int i=;i<=n+;i++)
{
if(i==n+)
x=;
else
cin>>x;
if(x>s.top().first)
s.push(make_pair(x,));
else
{
int w=;
while(s.top().first>x)
{
w=w+s.top().second;
ans=max(ans,(long long )w*(s.top().first));
s.pop();
}
s.push(make_pair(x,w+));
}
}
cout<<ans<<endl;
}
}

单调栈(POJ2559)的更多相关文章

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

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

  2. [POJ2559&POJ3494] Largest Rectangle in a Histogram&Largest Submatrix of All 1’s 「单调栈」

    Largest Rectangle in a Histogram http://poj.org/problem?id=2559 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题 ...

  3. ☆ [POJ2559] Largest Rectangle in a Histogram 「单调栈」

    类型:单调栈 传送门:>Here< 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题思路 单调栈的经典题 显然,最终的子矩形高度一定和某一个矩形相等(反证).因此一 ...

  4. poj2559/hdu1506 单调栈经典题

    我实在是太菜了啊啊啊啊啊 到现在连个单调栈都不会啊啊啊 写个经典题 #include<cstdio> #include<algorithm> #include<cstri ...

  5. poj2559 Largest Rectangle in a Histogram(单调栈)

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  6. POJ2559 Largest Rectangle in a Histogram 单调栈

    题目大意 有一个直方图,其所有矩形的底均是1(以后简称小矩形).给出这些矩形的高度,求这些矩形的并集中存在的面积最大的矩形(简称大矩形)的面积. 题解 大矩形的高必然一边等于一个小矩形的高,另一边小于 ...

  7. poj 2059 单调栈

    题意:求柱状图中最大矩形面积. 单调栈:顾名思义就是栈内元素单调递增的栈. 每次插入数据来维护这个栈,假设当前须要插入的数据小于栈顶的元素,那就一直弹出栈顶的元素.直到满足当前须要插入的元素大于栈顶元 ...

  8. poj1964最大子矩阵 (单调栈加枚举)

    题目传送门 题目大意: 一个矩阵中,求F组成的矩阵的面积,(答案乘以三). 思路:n如果是小于100的,就可以通过前缀和,然后三重循环暴力找,和poj1050很像,但由于是1000,就不可以了,时间复 ...

  9. 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 ...

  10. 51Nod 1158 全是1的最大子矩阵 —— 预处理 + 暴力枚举 or 单调栈

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1158 1158 全是1的最大子矩阵  基准时间限制:1 秒 空 ...

随机推荐

  1. Ubuntu下LAMP的环境配置教程

    总体来说,Ubuntu下安装LAMP环境是比较简单的,只需按照命令行执行即可,记录操作以备不时之需. 一,首先更新Ubuntu里面所有的软件 sudo apt-get update 二.之后安装Apa ...

  2. 小浩算法|一文让你学会如何用代码判断"24"点

    “24点”是一种数学游戏,正如象棋.围棋一样是一种人们喜闻乐见的娱乐活动.它始于何年何月已无从考究,但它以自己独具的数学魅力和丰富的内涵正逐渐被越来越多的人们所接受.今天就为大家分享一道关于“24点” ...

  3. Java中的合并与重组(上)

    通过优锐课核心java学习笔记中,我们可以看到,码了很多专业的相关知识, 分享给大家参考学习. 虽然在Git中合并和重组是相似的,但它们具有两种不同的功能. 要保持自己的历史记录整洁或完整,这是你应该 ...

  4. C#制作Wincc组件进行配方管理

    1,安装WinccV7.4并破解: 安装WinccV7.4SP1. 安装授权文件---根据提示 安装免狗驱动,根据提示 安装SImatic.net v13. 2,连接PLC, 首先在同一个局域网里面, ...

  5. Elasticsearch之增加和删除索引

    增加索引 利用postMan工具发送restfulAPI添加索引库 请求方式为put代表添加 创建索引index时映射mapping 请求URL: 使用put发送http://localhost:92 ...

  6. java开发病房管理系统

    开发环境: Windows操作系统开发工具: Myeclipse+Jdk+Tomcat+MySQL数据库 运行效果图 源码及原文链接:https://javadao.xyz/forum.php?mod ...

  7. docker入门整理(1)--安装

    1.安装批量命令: CentOS7操作系统下. 包含卸载旧版本.安装依赖包.添加最新Yum源.安装docker-ce最新稳定版本.启动docker等: sudo yum remove docker \ ...

  8. BUUCTF 部分wp

    目录 Buuctf crypto 0x01传感器 提示是曼联,猜测为曼彻斯特密码 wp:https://www.xmsec.cc/manchester-encode/ cipher: 55555555 ...

  9. Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set…

    php打印小票错误提示:Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activate ...

  10. 令人抓狂的redis和rediscluster Python驱动包的安装

    本文环境:centos 7,Python3编译安装成功,包括pip3,然后需要安装redis相关的Python3驱动包,本的redis指redis包而非redis数据库,rediscluster类似. ...