Largest Rectangle in a Histogram

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22968    Accepted Submission(s): 7175

Problem 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
 
Source
 
Recommend
LL   |   We have carefully selected several similar problems for you:  1505 1069 1087 1058 1176 
 

思路:

感觉太裸了点

还是单调栈,这回是个单调递增栈

因为如果以当前这个高度作为矩形的高度的话后面的矩形高度必须比他高

否则就不成立

那么我们维护一个单调递增栈

当他需要被弹出时,则说明以该高度为高的矩形走不下去了

那么我们就可以记下端点

记得正反跑两遍

最后用(右端点-左端点+1)*高度就是当前矩形面积

取max即可

(别忘了开long long)

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rii register int i
#define rij register int j
#define int long long
using namespace std;
int l[],r[],h[],n,cnt,stack[];
signed main()
{
while(~scanf("%lld",&n))
{
if(n==)
{
break;
}
for(rii=;i<=n;i++)
{
scanf("%lld",&h[i]);
}
cnt=;
for(rii=;i<=n;i++)
{
if(cnt==)
{
cnt++;
stack[cnt]=i;
continue;
}
if(h[stack[cnt]]<=h[i])
{
cnt++;
stack[cnt]=i;
}
else
{
while(h[stack[cnt]]>h[i])
{
r[stack[cnt]]=i-;
cnt--;
}
cnt++;
stack[cnt]=i;
}
}
while(cnt!=)
{
r[stack[cnt]]=n;
cnt--;
}
for(rii=n;i>=;i--)
{
if(cnt==)
{
cnt++;
stack[cnt]=i;
continue;
}
if(h[stack[cnt]]<=h[i])
{
cnt++;
stack[cnt]=i;
}
else
{
while(h[stack[cnt]]>h[i])
{
l[stack[cnt]]=i+;
cnt--;
}
cnt++;
stack[cnt]=i;
}
}
while(cnt!=)
{
l[stack[cnt]]=;
cnt--;
}
int ans=;
for(rii=;i<=n;i++)
{
ans=max(ans,(r[i]-l[i]+)*h[i]);
}
printf("%lld\n",ans);
}
}

最新文章

  1. CSS选择器 转
  2. 碉堡了! 纯 CSS 绘制《辛普森一家》人物头像
  3. Python 第一课
  4. Tr A(矩阵快速幂)
  5. while((c = getchar()) != EOF)(键盘输入问题)
  6. Hive QL
  7. Android:Context的作用
  8. Android Service 服务(一)—— Service .
  9. 反射实体自动生成EasyUi DataGrid模板
  10. (十)unity4.6学习Ugui中文文档-------參考-UGUI Canvas Components
  11. android布局margin和padding差异!
  12. Array.apply(null,{length:20})与new Array(20)的区别
  13. Java ee el表达式
  14. 动态主机配置协议DHCP
  15. EasyUI中, datagrid用loadData方法绑定数据。
  16. Android Studio 日志工具
  17. 【转】Android-Accessibility(辅助功能/无障碍,自动安装APP)
  18. Lua面向对象之二:类继承
  19. ArcGIS三种方式打断相交线------Planarize Lines工具
  20. 大量的源文件添加到Android.mk的问题

热门文章

  1. java.sql.SQLException: The server time zone value &#39;&#214;&#208;&#185;&#250;&#177;&#234;&#215;&#188;&#202;&#177;&#188; 解决方案
  2. iDempiere 使用指南 windows下eclipse开发环境配置及打包下载
  3. 使用notebook 笔记(1)
  4. 【起航计划 036】2015 起航计划 Android APIDemo的魔鬼步伐 35 App-&gt;Service-&gt;Messenger Service Messenger实现进程间通信
  5. canvas入门(一)
  6. 生成对抗式网络 GAN的理解
  7. php json_encode 中文乱码解决方法
  8. NO.005-2018.02.10《南歌子词二首 / 新添声杨柳枝词》唐代:温庭筠
  9. pip下载太慢解决方法
  10. bootstrap table 分页只显示分页不显示总页数等数据