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 ...
随机推荐
- Libxml2 学习
Libxml2 学习 1.概要 libxml 是一个实现操作XML数据功能的开源C语言库. API参考文档 http://xmlsoft.org/html/libxml-tree.html 2.wi ...
- C#简单实现读取txt文本文件并分页存储到数组
最近做一个VR项目,需要把某个中草药的介绍信息分页显示到unity场景里然后用VR手柄切换信息. unity的脚本是c#,就先在本地写了个代码测试了一下,利用控制台测试输出,到时候拷贝函数过去再结合交 ...
- BZOJ5249: [2018多省省队联测]IIIDX(线段树 贪心)
题意 题目链接 Sol 不难发现题目给出的是一个树,其中\(\frac{i}{K}\)是\(i\)的父亲节点 首先,当\(d_i\)互不相同时,一个显然的贪心策略就是优先给编号小的分配较大的权值.可以 ...
- C语言腾讯课堂(一)
腾讯课堂:c语言从零到精通 1. 从第一个例子开始 #include <stdio.h> int main(void) { printf("hello, qin men \n&q ...
- Linux C -> symlink 和 readlink -> 符号链接
Linux C -> symlink 和 readlink -> 符号链接 -------------------------------------------------------- ...
- mysql忘记root用户密码重置密码的方式
解决方法如下: 让mysql不载入权限表,命令:mysqld --skip-grant-tables(windows).mysqld_safe --skip-grant-tables user=mys ...
- MSSQL的SQL语句独立执行消耗与线上执行消耗差异
环境: SQL Server 2012 疑问:同样的一条语句,使用Profile跟踪出来的消耗与单独拿出来执行的消耗存在非常大的差距 语句如下: declare @str nvarchar(max) ...
- 扫描FTP,保存文件
1.需求:某公司ftp服务器中一个文件夹中有30个文件(文件名字是不同的),每五分钟产生一个新的文件,同时删除这三十个文件中最早产生的文件,该文件夹中始终保持30个文件. 现在需要采集一周的数据做研究 ...
- 部署Smokeping
1.环境安装 # yum -y install gcc* perl* httpd* libxml2 libxml2-devel libpng libpng-devel glib2 glib2-deve ...
- Linux--LAMP平台搭建
LAMP平台搭建 准备工作 rpm -e php php-cli php-ldap php-commn php-mysql --nodeps 删除php相关依赖软件 rpm -ivh zlib-dev ...