题目链接:http://poj.org/problem?id=2559

Largest Rectangle in a Histogram
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 24259   Accepted: 7844

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 follow n 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.

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 —— 单调栈的更多相关文章

  1. POJ2559 Largest Rectangle in a Histogram 单调栈

    题目大意 有一个直方图,其所有矩形的底均是1(以后简称小矩形).给出这些矩形的高度,求这些矩形的并集中存在的面积最大的矩形(简称大矩形)的面积. 题解 大矩形的高必然一边等于一个小矩形的高,另一边小于 ...

  2. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  3. POJ 2559 Largest Rectangle in a Histogram(单调栈)

    传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  4. POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15831 ...

  5. hdu 1506 Largest Rectangle in a Histogram(单调栈)

                                                                                                       L ...

  6. po'j2559 Largest Rectangle in a Histogram 单调栈(递增)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29498 ...

  7. HDU - 1506 Largest Rectangle in a Histogram (单调栈/笛卡尔树)

    题意:求一个直方图中最大矩形的面积. 很经典的一道问题了吧,可以用单调栈分别求出每个柱子左右两边第一个比它低的柱子(也就相当于求出了和它相连的最后一个比它高的柱子),确定每个柱子的左右边界,每个柱子的 ...

  8. PKU 2559 Largest Rectangle in a Histogram(单调栈)

    题目大意:原题链接 一排紧密相连的矩形,求能构成的最大矩形面积. 为了防止栈为空,所以提前加入元素(-1,0) #include<cstdio> #include<stack> ...

  9. 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 ...

随机推荐

  1. Codeforces 196C Paint Tree(贪心+极角排序)

    题目链接 Paint Tree 给你一棵n个点的树和n个直角坐标系上的点,现在要把树上的n个点映射到直角坐标系的n个点中,要求是除了在顶点处不能有线段的相交. 我们先选一个在直角坐标系中的最左下角的点 ...

  2. 洛谷—— P1440 求m区间内的最小值

    https://www.luogu.org/problemnew/show/P1440 题目描述 一个含有n项的数列(n<=2000000),求出每一项前的m个数到它这个区间内的最小值.若前面的 ...

  3. 2017-10-29-afternoon-清北模拟赛

    T1 洗澡 贪心:将未匹配的右括号花费1变为左括号,最有多余的左括号有一半变成右括号 #include <cstring> #include <cstdio> ); int n ...

  4. TCP server和client的一些测试

    一.TCP server和client测试   socket设置 测试项/测试情景 send recv 测             server block           client bloc ...

  5. -[__NSCFString longValue]: unrecognized selector sent to instance

    You can use NSString methods intValue longLongValue. 这个使用longvalue会crash报上面的错误

  6. SocketIO总结

    我在马克飞象上写的一样的内容,感觉那个样式好看的:WorkerMan的部分总结 workerman中部分函数总结 以下是把我搜集到的资料进行了一个整合.详细怎么使用.慢慢摸索吧. Worker类 中文 ...

  7. C++11 并发指南五(std::condition_variable 详解)(转)

    前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三(std::mutex 详解)>分别介绍了 std::thread,std::mut ...

  8. 基于Innobackupex的全备恢复

    对于MySQL数据库的热备,xtrabackup是大多数DBA朋友们的选择.xtrabackup内嵌了一个innobackupex可用于热备MySQL数据库.本文描写叙述了基于innobackupex ...

  9. python正则中的贪婪与非贪婪

    当重复一个正则表达式时,如用 a*,操作结果是尽可能多地匹配模式.当你试着匹配一对对称的定界符,如 HTML 标志中的尖括号.匹配单个 HTML 标志的模式不能正常工作,因为 .* 的本质是“贪婪”的 ...

  10. Hnu 11187 Emoticons :-) (ac自己主动机+贪心)

    题目大意: 破坏文本串.使之没有没有出现表情.破坏就是用空格替换.问最少须要破坏多少个字符. 思路分析: 初看跟Hdu 2457 没什么差别,事实上Hdu2457是要求将字符替换成ACGT,而这个仅仅 ...