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. DotNet 源码学习——QUEUE

    1.Queue声明创建对象.(Queue为泛型对象.) public class Queue<T> :IEnumerable<T>,System.Collections.ICo ...

  2. C语言:字符串拷贝(截取)、查找

    C语言:字符串拷贝(截取).查找 很惭愧,学了这么久别的语言,一直没有好好学C和C++,所以现在开始认真C/C++的一些特性和比较,这里记录下C语言拷贝和截取的一些方式,由于系统库带的函数不方便,所以 ...

  3. 1.3.6 详解build.gradle文件——Android第一行代码(第二版)笔记

    不同于Eclipse,Android Studio是采用Gradle来构建项目的.Gradle是一个非常先进的项目构建工具,它使用了一种基于Groovy的领域特定语言(DSL)来声明项目设置. 首先看 ...

  4. 11.Android-Xml读写

    android中写XML时,需要用到XmlSerializer类 解析XML时,则需要用到XmlPullParser类 1.XmlSerializer类介绍 通过Xml.newSerializer() ...

  5. xcode 11.3 发布ipa采坑记录

    为了适配ios13,特意更新了xcode11.3 .更新完后发现 application loader没有了,然后蒙了. 然后网上一顿搜索,归纳出了三种上传方式: 一.altool 使用xcode中的 ...

  6. Kafka消费者没有收到通知的分析

    今天遇到两位三方人员跟我反馈,某微服务的异步接口功能不正常了,由于该异步接口采用Kafka异步消息的方案,对方说没有收到Kafka给消费者的通知,根据此问题,联系了相关人员进行了分析: (一)明确环境 ...

  7. 第1章 JavaScript 简介

    第1章 JavaScript 简介 1.1 JavaScript简史 1.2 JavaScript实现 1.2.1 ECMAScript 1.2.2 文档对象模型(DOM) 1.2.3 浏览器对象模型 ...

  8. 用ArcGIS Runtime for Android建立简单App,展示地图

    1.新建AS项目 此处引用官网上新建项目的过程,很简单,不做翻译了. 2.配置ArcGIS Runtime for Android100.5.0环境 2-1.项目切换成Project 2-2 .选择P ...

  9. Umi 小白纪实(二)—— model 的注册与使用

    Umi 通常会搭配 Dva 使用,用于管理页面状态和逻辑 一.注册 model 首先需要在 .umirc.js 中启用 dva 插件 export default { plugins: [ ['umi ...

  10. .net生成PDF文件的几种方式

    以下为在.net mvc中,生成pdf的几种方式,资料都是在做项目时网上找的 1.使用Microsoft.Office.Interop.Word.dll将word转换为PDF dll可以单独下载,一般 ...