POJ2559 Largest Rectangle in a Histogram —— 单调栈
题目链接:http://poj.org/problem?id=2559
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 24259 | Accepted: 7844 |
Description

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
Output
Sample Input
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0
Sample Output
8
4000
Hint
Source
题解:
一开始想到的是;从当前元素,向前向后延伸,计算最多能延伸多长。O(n^2)超时~~~~~
然后看了网上的题解:单调栈。
如果当前元素大于栈顶元素,则入栈;否则,栈顶元素出栈,直到栈顶元素小于当前元素或栈为空为止。期间更新出栈元素能达到的宽度,因为上一次出栈的元素的高度必定大于这次出栈元素的高度,所以他对这次出栈元素的宽度有贡献,并更新ans。出栈完毕后,再将已出栈元素的总宽度叠加到当前元素中,因为这些元素的高度都大于等于当前元素的高度,所以这些元素对当前元素的宽度都有贡献。 最后栈中可能还会有元素,把他“倒”出来再统计就是了。
代码如下:
#include<cstdio>//poj2559 单调栈 此栈从栈底到栈顶为严格递增
#include<cstring>
#define MAX(a,b) (a>b?a:b)
#define LL long long
#define mod 1000000007 using namespace std; struct Stack
{
int he;
int len;
}s[]; int main()
{
int n,h;
while(scanf("%d",&n) && n)
{
LL ans = -;
int top = ;//从1开始存放,top指向栈顶元素
for(int i = ; i<=n; i++)
{
scanf("%d",&h);
int len = ;
//当栈顶元素的高度大于等于当前将入栈的元素的高度时,出栈,并更新其之前元素的宽度
while(top> && h<=s[top].he)
{
len += s[top].len;
ans = MAX(ans,1LL*s[top].he*len);
top--;
}
//更新入栈元素的宽度
s[++top].len = len + ;
s[top].he = h;
} int len = ;
while(top>)//将栈内剩余的元素“倒”出来统计
{
len += s[top].len;
ans = MAX(ans,1LL*len*s[top].he);
top--;
} printf("%lld\n",ans);
}
return ;
}
POJ2559 Largest Rectangle in a Histogram —— 单调栈的更多相关文章
- POJ2559 Largest Rectangle in a Histogram 单调栈
题目大意 有一个直方图,其所有矩形的底均是1(以后简称小矩形).给出这些矩形的高度,求这些矩形的并集中存在的面积最大的矩形(简称大矩形)的面积. 题解 大矩形的高必然一边等于一个小矩形的高,另一边小于 ...
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...
- POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15831 ...
- hdu 1506 Largest Rectangle in a Histogram(单调栈)
L ...
- po'j2559 Largest Rectangle in a Histogram 单调栈(递增)
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29498 ...
- HDU - 1506 Largest Rectangle in a Histogram (单调栈/笛卡尔树)
题意:求一个直方图中最大矩形的面积. 很经典的一道问题了吧,可以用单调栈分别求出每个柱子左右两边第一个比它低的柱子(也就相当于求出了和它相连的最后一个比它高的柱子),确定每个柱子的左右边界,每个柱子的 ...
- PKU 2559 Largest Rectangle in a Histogram(单调栈)
题目大意:原题链接 一排紧密相连的矩形,求能构成的最大矩形面积. 为了防止栈为空,所以提前加入元素(-1,0) #include<cstdio> #include<stack> ...
- Largest Rectangle in a Histogram /// 单调栈 oj23906
题目大意: 输入n,,1 ≤ n ≤ 100000,接下来n个数为每列的高度h ,0 ≤ hi ≤ 1000000000 求得最大矩阵的面积 Sample Input 7 2 1 4 5 1 3 34 ...
随机推荐
- AC自动机(加强版)
题目描述 有NN个由小写字母组成的模式串以及一个文本串TT.每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串TT中出现的次数最多. 输入输出格式 输入格式: 输入含多组数据. 每组数据 ...
- [simple-orm-mybaits]基于Mybatis的ORM封装介绍
目录 前言 ORM框架现状 Mybatis优缺点 simple-orm-mybatis设计思路介绍 simple-orm-mybatis使用说明 simple-orm-mybatis实际使用 推荐最佳 ...
- Hibernate 3 深度解析--苏春波
Hibernate 3 深度解析 Hibernate 作为 Java ORM 模式的优秀开源实现, 当下已经成为一种标准,为饱受 JDBC 折磨的 Java 开发者带来了“福音.快速的版本更新,想 ...
- Android View 布局流程(Layout)完全解析
前言 上一篇文章,笔者详细讲述了View三大工作流程的第一个,Measure流程,如果对测量流程还不熟悉的读者可以参考一下上一篇文章.测量流程主要是对View树进行测量,获取每一个View的测量宽高, ...
- DEDECMS5.5怎样调用{dede:field.content/}做简介之类的单独页面?
很多时候,如果用dede来做一些企业公司网站,或者一些部门网站的时候.需要某些栏目是一个单页的文章,用于公司简介或者企业文化之类的.那么就要用到栏目功能的栏目内容,也就是dede的content标签. ...
- C# 通过WebService方式 IIS发布网站 上传文件到服务器[转]
http://blog.sina.com.cn/s/blog_517cae3c0102v0y7.html 应用场景:要将本地的文件 上传到服务器的虚拟机上 网络环境:公司局域网(如下图中第二种) 开发 ...
- 如何提高NodeJS程序的运行的稳定性
如何提高NodeJS程序运行的稳定性 我们常通过node app.js方式运行nodejs程序,但总有一些异常或错误导致程序运行停止退出.如何保证node程序的稳定运行? 下面是一些可以考虑的方案: ...
- eclipse中文凝视字体太小解决方法
新安装的eclipse中文凝视字体太小.解决方法例如以下: 打开Elcipse-->点击菜单条上的"Windows"-->点击"Preferences&quo ...
- Solaris网络基础
划分子网: 把大网缩小为若干个小网.修改子网掩码,划分多个网络. 那么如何确定子网的子网掩码和IP地址? 以上你会发现少了6个IP. Ifconfig e1000g0 down down掉网卡 ...
- PHP date()获取系统时间不对
使用date_default_timezone_set(”)方法; <?php error_reporting(0); date_default_timezone_set('PRC'); hea ...