题目链接: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. es6系列-变量声明

    es6系列所有文章都是阅读阮一峰老师的<ES6标准入门>(第2版)所做的读书笔记.方便日后查阅相关基础知识. git地址: https://github.com/rainnaZR/es6- ...

  2. http://blog.csdn.net/zh521zh/article/details/52687922

    http://blog.csdn.net/zh521zh/article/details/52687922

  3. fastscripT实现权限控制

    fastscripT权限控制 此处以FASTSCRIPT实现功能权限为例,用脚本实现数据权限也是很方便的. unit Unit1; interface uses Winapi.Windows, Win ...

  4. 阿里云云服务器ubuntu配置nginx+uwsgi+django记录文档

    1 安装ssh 1  sudo apt-get update 2  sudo apt-get install openssh-server 3  sudo ps -e |grep ssh  有sshd ...

  5. [反汇编练习] 160个CrackMe之035

    [反汇编练习] 160个CrackMe之035. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  6. Raid分类说明 (from mongodb权威指南)

    RAID(Redundant Array of Independent Disk,独立磁盘冗余阵列,旧称Redundant Array of InexpensiveDisk,廉价磁盘冗余阵列) 是一种 ...

  7. Windows 无法卸载IE9怎么办

    1 如下图所示,使用自带的卸载工具无法卸载IE9 运行命令提示符,粘贴下面的命令 FORFILES /P %WINDIR%\servicing\Packages /M Microsoft-Window ...

  8. JSP自己定义标签继承哪个类

    JSP自己定义标签继承哪个类 解:JSP自己定义标签继承TagSupport

  9. 【软件创意】智能Goals (android)

    智能Goals  软件创意核心思想:实现你的愿望. 功能概要:帮助记录奋斗了的历程.实现你的愿望.可以是跑步减肥,每天阅读,交际,存钱买房.满足各种记录需要,目标可以是完成多长时间,可以用计时器:可以 ...

  10. C语言函数的递归和调用

    函数记住两点: (1)每个函数运行完才会返回调用它的函数:每个函数运行完才会返回调用它的函数,因此,你可以先看看这个函数不自我调用的条件,也就是fun()中if条件不成立的时候,对吧,不成立的时候就是 ...