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. [LeetCode]21. Merge Two Sorted Lists合并两个有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  2. springboot如何实现微信登录,前期准备

    现在网站用微信登录真的是很多,那么具体是怎么实现的呢? 首先介绍的是微信开放平台,我们如果需要微信登录或者支付都需要在上面注册一个账号,用这个账号去为我们的网站申请的话,需要用到企业资料(家里有营业执 ...

  3. 希尔排序——Java实现

    一.排序思想 希尔排序(Shell’s Sort)是插入排序的一种,是直接插入排序算法的一种更高版本的改进版本. 把记录按步长gap分组,对每组记录采用直接插入排序方法进行排序: 随着步长逐渐减小,所 ...

  4. hibernate的查询 (比较get 与load)

    hibernate的查询的比较hibernate的查询有很多,Query,find,Criteria,get,load query使用hsql语句,可以设置参数是常用的一种方式 criteria的方式 ...

  5. Android-事件处理机制(待补充)

    http://www.cnblogs.com/plokmju/archive/2013/03/13/2955175.html Android有两条事件处理机制 基于监听的事件处理 基于回调的事件处理 ...

  6. Hibernate中的一对一注解配置

    Card类 package cn.OneToOne2017109.entity; import javax.persistence.*; /** * Created by YSS on 2017/10 ...

  7. Spring课程 Spring入门篇 4-6 Spring bean装配之基于java的容器注解说明--@ImportResource和@Value java与properties文件交互

    1 解析 1.1 这两个注解应用在什么地方 1.2 应用方式 1.3 xml方式实现取值 2 代码演练 2.1 @ImportResource和@Value代码演练 1 解析 1.1 这两个注解应用在 ...

  8. 一个基于Ionic3.x cordova的移动APP demo

    项目地址如遇网络不佳,请移步国内镜像加速节点 前端技术: Angular4.x + ionic3.x + cordova 项目运行: git clone git@github.com:EasyTuan ...

  9. scss-@mixin传参

    混合器一个很重要特性就是可以传递参数,可以根据不同场景来定制css代码的复用.极大提高了混合器的适用性,看如下scss代码实例: @mixin makeradius($radius) { border ...

  10. js实现瀑布流加载图片效果

    今天学习了一个瀑布流加载效果,很多网站都有瀑布流效果,瀑布流就是很多产品显示在网页上,宽相同,高度不同,表现为多栏布局,随着页面滚动条向下滚动,这种布局还会不断加载数据块并附加至当前尾部.原理是:1. ...