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 ...
随机推荐
- codevs——1700 施工方案第二季
1700 施工方案第二季 2012年市队选拔赛北京 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description c国边防 ...
- 随笔:Golang 时间Time
先了解下time类型: type Time struct { // sec gives the number of seconds elapsed since // January 1, year 1 ...
- DELPHI 10.2(TOKYO) FOR LINUX的兼容性说明
DELPHI 10.2(TOKYO) FOR LINUX的兼容性说明 自DELPHI 10.2(TOKYO) 始开始支持Linux . Delphi Linux 编译器 64 位 Linux 平台支持 ...
- angular http ajax header
myAppModule.config(['$httpProvider', function($httpProvider) { $httpProvider.defaults.headers.common ...
- from: 关于RabbitMQ
from: http://lynnkong.iteye.com/blog/1699684 1 什么是RabbitMQ? RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种, ...
- webstorm中使用java的块凝视
webstorm中使用java的块凝视 有图有真相 使用java中非常方便得/**+enter就能高速创建类凝视.可是webstrom中没有现成的,所以我们得自己去创建一个,详细方法例如以下: 博主w ...
- SwitchyOmega 代理设置
1.SwitchyOmega官网 https://www.switchyomega.com/ 2.下载插件 https://www.switchyomega.com/download.html 3.配 ...
- Populating Next Right Pointers in Each Node I, II——生成next树
1. Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- One usage of recurison: the tower of Hanoi
Statements: This blog was written by me, but most of content is quoted from book[Data Structure wit ...
- 用户'sa'登录失败(错误18456)解决方案图解
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://thenear.blog.51cto.com/4686262/865544 htt ...