Largest Rectangle in a Histogram(hdu1506,单调栈裸题)
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

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.
4 1000 1000 1000 1000
0
4000
思路:
感觉太裸了点
还是单调栈,这回是个单调递增栈
因为如果以当前这个高度作为矩形的高度的话后面的矩形高度必须比他高
否则就不成立
那么我们维护一个单调递增栈
当他需要被弹出时,则说明以该高度为高的矩形走不下去了
那么我们就可以记下端点
记得正反跑两遍
最后用(右端点-左端点+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,单调栈裸题)的更多相关文章
- 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 ...
- poj 2559 Largest Rectangle in a Histogram (单调栈)
http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 6 ...
- poj2559 Largest Rectangle in a Histogram(单调栈)
Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...
- 题解报告: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 ...
- Largest Rectangle in a Histogram【单调栈模板】
Largest Rectangle in a Histogram 题目链接(点击)来源poj 2559 A histogram is a polygon composed of a sequence ...
- POJ2559 Largest Rectangle in a Histogram (单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26012 ...
- hdu_1506:Largest Rectangle in a Histogram 【单调栈】
题目链接 对栈的一种灵活运用吧算是,希望我的注释写的足够清晰.. #include<bits/stdc++.h> using namespace std; typedef long lon ...
- ☆ [POJ2559] Largest Rectangle in a Histogram 「单调栈」
类型:单调栈 传送门:>Here< 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题思路 单调栈的经典题 显然,最终的子矩形高度一定和某一个矩形相等(反证).因此一 ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
[题目链接] http://poj.org/problem?id=2559 [题目大意] 给出一些宽度为1的长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题 ...
- 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 ...
随机推荐
- 02.for循环
语法: for(表达式1;表达式2;表达式3) { 循环体; } 练习1: namespace _02.for循环的练习01 { class Program { static void Main(st ...
- 初学orcale(一)
Oracle数据库学习: 01.数据库简介: (1)文件型数据库: Access Office组件: Foxpro (2)NoSql数据库(泛指非关系型数据库): NoSQL(NoSQL = Not ...
- HTML表单特别效果—音量调节,购物数量
<form oninput="x.value=parseInt(a.value)+parseInt(b.value)">0<input type="ra ...
- 上传文件到Maven仓库
1.上传jar到本地仓库 mvn install:install-file -DgroupId=org.csource -DartifactId=fastdfs-client-java -Dversi ...
- setExecuteExistingDelayedTasksAfterShutdownPolicy方法与setContinueExistingPeriodicTasksAfterShutdownPolicy方法的比较
一.setExecuteExistingDelayedTasksAfterShutdownPolicy方法 这个方法大多是与schedule方法和shutdown方法搭配使用的. public voi ...
- XP无法访问SharePoint 2010的问题
最近用户反馈XP系统的电脑无法访问SharePoint 2010系统,换成了火狐浏览器后可以正常访问,但是在上传附件时出现异常,支持人员给的解决方案是换操作系统,要换成win7. 但是因为工作原因,不 ...
- 安全隐患,你对X-XSS-Protection头部字段理解可能有误
0×00. 引言 我曾做过一个调查,看看网友们对关于X-XSS-Protection 字段的设置中,哪一个设置是最差的,调查结果令我非常吃惊,故有此文. 网友们认为 最差的配置是X-XSS-Prote ...
- 从java9开始就不再提供32位jdk
- Golang Gin 项目使用 Swagger
Golang Gin 项目使用 Swagger 标签(空格分隔): Go 首先需要github.com/swaggo/gin-swagger和github.com/swaggo/gin-swagger ...
- ccsu小助手
CCSU小助手 队名:瓜队 组员:钟文兴.周畅.吉刘磊.唐仲勋 宣言:We are a team at any time! 团队项目描述: 内容:“生活在长大”: 目标:为了方便对学校不了解的学生能够 ...