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. oracle系列练习题

    刚学习Oracle,老师给我们布置了一些题目来练习. 题目: 创建四个表,并录入数据 1.student表 CREATE TABLE STUDENT (SNO VARCHAR(3) NOT NULL, ...

  2. iOS开发基础--C语言简述(一)

    先占个坑,回来再补 需要的运行环境,自行搜寻,工具不止一种,不详细叙述. C语言是一门非常重要的编程语言,与硬件底层直接相关,很多语言到最后的接口封装都会选择C语言,因而C语言一直很受欢迎,也务必掌握 ...

  3. 【Python3爬虫】一次应对JS反调试的记录

    一.前言简介 在前面已经写过关于 JS 反调试的博客了,地址为:https://www.cnblogs.com/TM0831/p/12154815.html.但这次碰到的网站就不一样了,这个网站并不是 ...

  4. [MongoDB]评估使用mongodb的五个因素

    企业选择 NOSQL 或非表格结构数据库,评估时应从以下五个关键维度来考虑:• 数据模型的类型• 查询模型是否能满足灵活的查询需求• 事务模型类型,以及一致性属于强一致性还是最终一致性• APIs 的 ...

  5. NodeJs-promise和async_await语法

    Callback hell回调地域 当我们以同步的方式编写耗时的代码,那么就会阻塞JS的单线程,造成CPU一直等待IO完成才去执行后面的代码. 而CPU的执行速度是远远大于硬盘IO速度的,这样等待只会 ...

  6. codeforces 995C

    题意:从L到R 找有几个x,使x=a^p(a>0,p>1) 题解: 一开始把所有符合的次方都存到vector,然后MLE 可以看到1e6^3=1e18,所以可以将二次方单独来求,其他次方存 ...

  7. #《Essential C++》读书笔记# 第一章 C++ 编程基础

    前言 Stanley B.Lippman 先生所著的<C++ Primer>是学习C++的一本非常优秀的教科书,但<C++ Primer>作为一本大部头书,显然不适合所有的初学 ...

  8. Java基础之五、Java编程思想(1-7)

    一.对象导论 1:多态的可互换对象 面向对象程序设计语言使用了后期绑定的概念. 当向对象发送消息时,被调用的代码直到运行时才能确定.也叫动态绑定. 2:单根继承结构 所有的类最终都继承自单一的基类,这 ...

  9. springboot整合WebService简单版

    一.什么是webservice 关于webservice的介绍摘自百度百科,上面的介绍很详细.(链接:https://baike.baidu.com/item/Web%20Service/121503 ...

  10. Eclipse 如何添加 更换字体(转载)

    1. 打开eclipse-->Window-->Preferences-->General-->appearance-->Colors and Fonts, 点开后选择B ...