po'j2559 Largest Rectangle in a Histogram 单调栈(递增)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 29498 | Accepted: 9539 |
Description

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
Output
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
Hint
//单调递增栈
#include<iostream>
#define ll long long
#include<stack>
using namespace std;
stack<ll>p; //栈里面存的是下标
ll a[];
ll n,s,top;
int main()
{
while(~scanf("%lld",&n))
{
if(n==)
break;
while(!p.empty())
p.pop();
for(int i=;i<n;i++)
scanf("%lld",&a[i]);
a[n]=-;//为找比a[n-1]小的数准备,因为是递增栈,将a[n]设为最小值
s=;
for(int i=;i<=n;i++)
{
if(p.empty()||a[i]>=a[p.top()])//看题目要求是否要严格单调递增,这里只要求递增
p.push(i);
else
{
while(!p.empty()&&a[i]<a[p.top()])//找到第一个小于栈顶元素的数的下标
{
top=p.top();
p.pop();//只在在出栈的过程中以a[top]为最小值更新面积s
if(s<(i-top)*a[top])
s=(i-top)*a[top];
}
p.push(top);//只将延伸到最左端的元素入栈,并且以最左端的元素的!坐标!为起点,找下一个比a[i]大的最长增区间
a[top]=a[i];//修改该位置的值为a[i] }
}
printf("%lld\n",s);
}
return ; }
po'j2559 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 ...
- hdu 1506 Largest Rectangle in a Histogram(单调栈)
L ...
- POJ2559 Largest Rectangle in a Histogram —— 单调栈
题目链接:http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Lim ...
- HDU - 1506 Largest Rectangle in a Histogram (单调栈/笛卡尔树)
题意:求一个直方图中最大矩形的面积. 很经典的一道问题了吧,可以用单调栈分别求出每个柱子左右两边第一个比它低的柱子(也就相当于求出了和它相连的最后一个比它高的柱子),确定每个柱子的左右边界,每个柱子的 ...
- POJ2559 Largest Rectangle in a Histogram 单调栈
题目大意 有一个直方图,其所有矩形的底均是1(以后简称小矩形).给出这些矩形的高度,求这些矩形的并集中存在的面积最大的矩形(简称大矩形)的面积. 题解 大矩形的高必然一边等于一个小矩形的高,另一边小于 ...
- PKU 2559 Largest Rectangle in a Histogram(单调栈)
题目大意:原题链接 一排紧密相连的矩形,求能构成的最大矩形面积. 为了防止栈为空,所以提前加入元素(-1,0) #include<cstdio> #include<stack> ...
- Largest Rectangle in a Histogram /// 单调栈 oj23906
题目大意: 输入n,,1 ≤ n ≤ 100000,接下来n个数为每列的高度h ,0 ≤ hi ≤ 1000000000 求得最大矩阵的面积 Sample Input 7 2 1 4 5 1 3 34 ...
随机推荐
- 获取iPod library中的媒体文件
[获取iPod library中的媒体文件] The Media Player framework provides facilities for playing movie, music, audi ...
- jquery.fn.extend() 与 $.jquery 作用及区别
原文:http://www.cnblogs.com/liu-l/p/3928373.html jQuery.extend()这个方法,主要是用来拓展个全局函数啦,例如$.ajax()这种,要不就是拓展 ...
- Android-startService后台运行
什么时候使用startService? 答:APP在后台长时间运行并执行业务的时候,可以用服务,主要是看服务的特点(在后台长时间运行): Service相关代码: package liudeli.se ...
- 使用Student T'test方法做性能测试
性能测试 日常工作中对比函数间的快慢时,最直接的方法就是根据timer:tc/1结果的时间来衡量,比如想知道lists:reverse/1与直接使用自己写的尾递归函数谁更快?最直接的方法就是 -mod ...
- Solr相似度算法三:DRFSimilarity框架介绍
地址:http://terrier.org/docs/v3.5/dfr_description.html The Divergence from Randomness (DFR) paradigm i ...
- 配置ssh使用socks代理
ssh -o ProxyCommand='nc -x 127.0.0.1:1080 %h %p' username@server
- Newtonsoft.Json Deserialize Type 或者 同类型 变量 反序列化
Newtonsoft.Json 经常再用 这样的需求 还是很少用 场景 方法一:根据 Type 反序列化 ; string jsongString = JsonConvert.SerializeObj ...
- Android 如何查看源码 (eclipse 按住 ctrl )
首先要确认 Android SDK Manager 下载并安装了 sources.然后在代码中按住 ctrl 在点一个类名, 如果打开的页面是找不到源码,就点那个按钮,然后找到源码所在的文件夹,就可以 ...
- 七、linux目录结构知识---实战
1.企业面试题:一个100M的磁盘分区,分别写入1k文件,及写入1M的文件,分别可以写多少个? 一块磁盘被分区格式化成系统文件后,有Inode和Block:一个文件一般占用一个Inode和一个Bloc ...
- “全栈2019”Java异常第二十章:自定义异常详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...