POJ 2559 Largest Rectangle in a Histogram(单调栈)
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 follown 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
Hint
Huge input, scanf is recommended.
思路
因为每个矩形的宽都为1,高不等,要求拼接起来的矩形的面积的最大值,可以看做给定一列数,定义子区间的值为区间长度乘以区间最小值,求区间值最大为多少。直接枚举肯定T,所以以每个值为区间最小值,向左向右扩展延伸区间,然后更新最大值,也就是单调栈的思想。如果当前元素大于栈顶元素,那么这个元素是不能向前伸展的;如果当前元素小于栈顶元素,这个时候就要把栈中的元素一个一个弹出来,直到当前元素大于栈顶元素,对于弹出来的元素,它扩展到当前元素就不能向后伸展下去了,因此对于弹出来的元素这个时候就可以计算左右端点形成区间与最小值的乘积了,维护一个最大值就好了。
#include<stdio.h> #include<string.h> typedef __int64 LL; const int maxn = 100005; LL a[maxn],stack[maxn],left[maxn]; int main() { int N; while (~scanf("%d",&N) && N) { LL res = 0,tmp; memset(stack,0,sizeof(stack)); memset(left,0,sizeof(left)); for (int i = 1;i <= N;i++) scanf("%I64d",&a[i]); a[++N] = -1; //手动加上“-1”,使得所有元素都能入栈出栈 int top = 0; for (int i = 1;i <= N;i++) { if (!top || a[i] > a[stack[top-1]]) { stack[top++] = i; left[i] = i; continue; } if (a[i] == a[stack[top-1]]) continue; while (top > 0 && a[i] < a[stack[top-1]]) { top--; tmp = a[stack[top]]*((i-1)- (left[stack[top]]-1)); res = res<tmp?tmp:res; } tmp = stack[top]; stack[top++] = i; left[i] = left[tmp]; } printf("%I64d\n",res); } return 0; }
POJ 2559 Largest Rectangle in a Histogram(单调栈)的更多相关文章
- 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 (单调栈或者dp)
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15831 ...
- PKU 2559 Largest Rectangle in a Histogram(单调栈)
题目大意:原题链接 一排紧密相连的矩形,求能构成的最大矩形面积. 为了防止栈为空,所以提前加入元素(-1,0) #include<cstdio> #include<stack> ...
- [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)
[POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...
- stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram
题目传送门 /* 题意:宽度为1,高度不等,求最大矩形面积 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极 ...
- poj 2559 Largest Rectangle in a Histogram 栈
// poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...
- 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 —— 单调栈
题目链接:http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Lim ...
- 题解报告: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 ...
随机推荐
- BASE64 编码和解码
依赖jar: import org.apache.commons.codec.binary.Base64; BASE64和其他相似的编码算法通常用于转换二进制数据为文本数据,其目的是为了简化存储或传输 ...
- 用jQuery File Upload做的上传控件demo,支持同页面多个上传按钮
需求 有这么一个需求,一个form有多个文件要上传,但又不是传统的图片批量上传那种,是类似下图这种需求,一开始是用的swfupload做的上传,但是问题是如果有多个按钮的话,就要写很多重复的代码,于为 ...
- Java 的世界,我不懂:奇葩的 json 序列化
先上张图,代表我心中的十万头草泥马: 写这么长的代码,头回见数组和单个实体共用同一个 json 节点的! 恐怕只有 java 社区的大牛B 才能做出这等事.. 由 Apache 发布: http:// ...
- 青瓷引擎之纯JavaScript打造HTML5游戏第二弹——《跳跃的方块》Part 10(排行榜界面&界面管理)
继上一次介绍了<神奇的六边形>的完整游戏开发流程后(可点击这里查看),这次将为大家介绍另外一款魔性游戏<跳跃的方块>的完整开发流程. (点击图片可进入游戏体验) 因内容太多,为 ...
- WebApiTestClient自定义返回值说明
WebApiTestClient是基于微软HelpPage一个客户端调试扩展工具,用来做接口调试比较方便.但是对返回值的自定义说明还是有缺陷的.有园友写过一篇文章,说可以通过对类进行注释,然后通过在I ...
- 在SQL Server 2012中实现CDC for Oracle
在上篇在SSIS 2012中使用CDC(数据变更捕获)中,介绍了如何在SSIS 2012中使用CDC,本文在此基础上介绍,如何通过Attunity提供的Change Data Capture Desi ...
- [BZOJ1564][NOI2009]二叉查找树(区间DP)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1564 分析: 首先因为每个点的数据值不变,所以无论树的形态如何变,树的中序遍历肯定不变 ...
- bat批处理文件启动Eclipse和ivy本地仓库的配置
一.bat批处理文件启动Eclipse 所需文件: 1.eclipse 2.jre 3.startup-eclipse.bat 确保以上三个文件夹同级 startup-eclipse.bat: set ...
- linux基础-第十七单元 Samba服务
Samba的功能 Samba的安装 Samba服务的启动.停止.重启 Samba服务的配置 Samba服务的主配置文件 samba服务器配置实例 Samba客户端设置 windows客户端 Linux ...
- android定时器
Handler+Timer+TimerTask 三.采用Handler与timer及TimerTask结合的方法. 1.定义定时器.定时器任务及Handler句柄 private final Time ...