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);
}
}

Largest Rectangle in a Histogram(hdu1506,单调栈裸题)的更多相关文章

  1. HDU-1506 Largest Rectangle in a Histogram【单调栈】

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

  2. poj 2559 Largest Rectangle in a Histogram (单调栈)

    http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 6 ...

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

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

  4. 题解报告:poj 2559 Largest Rectangle in a Histogram(单调栈)

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

  5. Largest Rectangle in a Histogram【单调栈模板】

    Largest Rectangle in a Histogram 题目链接(点击)来源poj 2559 A histogram is a polygon composed of a sequence ...

  6. POJ2559 Largest Rectangle in a Histogram (单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26012 ...

  7. hdu_1506:Largest Rectangle in a Histogram 【单调栈】

    题目链接 对栈的一种灵活运用吧算是,希望我的注释写的足够清晰.. #include<bits/stdc++.h> using namespace std; typedef long lon ...

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

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

  9. POJ 2559 Largest Rectangle in a Histogram(单调栈)

    [题目链接] http://poj.org/problem?id=2559 [题目大意] 给出一些宽度为1的长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题 ...

  10. HDU 1506 Largest Rectangle in a Histogram(单调栈、笛卡尔树)

    题意:给定n个连续排列的矩形的高,矩形的宽都为1.问最大矩形覆盖. 例如:n = 7,h[i] = (2 1 4 5 1 3 3),最大覆盖为8. Sample Input 7 2 1 4 5 1 3 ...

随机推荐

  1. DataRow获取数值类型为空或NULL时异常处理

    //获取数据集内容 DataSet ContractDS = dal.GetJHFKStr(jhfubh); //验证数据集是否为空 if (!DataSetUtil.IsNullOrEmpty(Co ...

  2. [JAVA小项目]GUI界面的局域网聊天室

    思路: 1.服务端: 1.1 创建ServerSocket 监听本地端口 1.2 循环接收多个客户端的连接,并且把多个客户端连接的每个管道都为其创建线程. 服务端类的成员:链表--每个成员都是线程类- ...

  3. python30题

    1.执行Python 脚本的两种方式 使用python解释器(python aa.py)或在unix系统下赋值成777,执行(./aa.py) 2.简述位.字节的关系 1个byte = 8bit,在A ...

  4. python数据类型(数字\字符串\列表)

    一.基本数据类型——数字 1.布尔型 bool型只有两个值:True和False 之所以将bool值归类为数字,是因为我们也习惯用1表示True,0表示False. (1)布尔值是False的各种情况 ...

  5. ASP.NET全局异常处理

    Web项目部署后,异常直接暴露给用户会产生很不好的体验.只是暴露在服务器端又无法实时记录异常原因以便加以重现并修复.所以配合Log4Net记录日志信息,同时全局异常处理来营造良好用户体验就比较重要了. ...

  6. js 中有关字符串的操作

    1. substring(start, end) 1). 包头不包尾 2). start 必需项 3). end 非必需项 4). start end 谁大谁小无所谓 5). start end 若为 ...

  7. C#操作CAD-调用winform

    个人认为用命令操作cad会比较便捷,但是鉴于好多人喜欢通过鼠标点击的方式操作cad,在此讲一下如何调用winform.前期准备请看上篇文章. 1.在新建好项目并引用接口dll的前提下,新建一个winf ...

  8. jQuery Event.stopImmediatePropagation() 函数详解

    stopImmediatePropagation()函数用于阻止剩余的事件处理函数的执行,并防止当前事件在DOM树上冒泡. 根据DOM事件流机制,在元素上触发的大多数事件都会冒泡传递到该元素的所有祖辈 ...

  9. java maven项目 导入jar包注意

    1.将jar 包放到lib目录下 2.build path 3.点击maven主项目右键选择properties 4.选择myeclipse 下的第一个选项 5.点击add   选择archives ...

  10. #单元测试#以karma+mocha+chai 为测试框架的Vue webpack项目(二)

    学习对vue组件进行单元测试,先参照官网编写组件和测试脚本. 1.简单的组件 组件无依赖,无props 对于无需导入任何依赖,也没有props的,直接编写测试案例即可. /src/testSrc/si ...