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

Solution

方法一:记录左、右第一个比每个元素低的元素

1.从左往右做单增栈,弹栈时r[stack[top--]]=当前元素地址,即各个元素右侧第一个比其低的元素地址;最后清栈时站内元素的r为n+1;

2.从右往左做单增栈,弹栈时l[stack[top--]]=当前元素地址,即各个元素左侧第一个比其低的元素地址;最后清栈时站内元素的l为0;

3.对于每个元素计算其对应的最大面积max[i]=h[i]*(r[i]-l[i]),对所有max[i]取最大值即可;

鉴于本方法便于自己实现,再此不给出对应代码;

方法二:从左往右或从右往左进行一次单增栈,每次弹栈时更新最大面积

1.栈内每个单位存入两个元素:该单位高度height和对应可控宽度length,对于每个大于栈顶直接入栈的元素,stack[i].length=1;

2.对于需要先弹栈再入栈的元素,其length=弹栈所有元素length之和+1,因为被弹栈的元素的高度均≥当前元素,所以其可控范围应加上被其弹栈元素的length;

3.在弹栈过程中,记录一个temp为本次弹栈到当前为止弹出的宽度,因为为单增栈,所以每个高度均可控其后被弹栈元素的宽度,所以其对应的面积为s=temp*h[i],取max更新ans即可;

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std; struct node{
long long height,length;
}stack[100100];
long long n,m,i,j,k,h[100100]; inline long long read(){
long long x=0;
bool f=true;
char c;
c=getchar();
while(c<'0'||c>'9'){
if(c=='-') f=false;
c=getchar();
}
while(c>='0'&&c<='9'){
x=(x<<1)+(x<<3)+(c^48);
c=getchar();
}
return f?x:-x;
} void calc(){
long long top=1,maxs=0,temp=0;
for(i=1;i<=n;++i) h[i]=read();
stack[1].height=h[1];
stack[1].length=1;
for(i=2;i<=n;++i){
temp=0;
while(stack[top].height>=h[i]&&top>0){
temp+=stack[top].length;
maxs=max(maxs,stack[top--].height*temp);
}
stack[++top].height=h[i];
stack[top].length=temp+1;
}
temp=0;
while(top>0){
temp+=stack[top].length;
maxs=max(maxs,stack[top--].height*temp);
}
printf("%lld\n",maxs);
return;
} int main(){
for(;;){
n=read();
if(!n)return 0;
calc();
}
return 0;
}

单调栈基础知识部分可以参考我的题解:http://www.cnblogs.com/COLIN-LIGHTNING/p/8474668.html

[POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)的更多相关文章

  1. poj 2559 Largest Rectangle in a Histogram (单调栈)

    http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 6 ...

  2. 题解报告: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 ...

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

    [题目链接] http://poj.org/problem?id=2559 [题目大意] 给出一些宽度为1的长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题 ...

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

    嗯... 题目链接:http://poj.org/problem?id=2559 一.单调栈: 1.性质: 单调栈是一种特殊的栈,特殊之处在于栈内的元素都保持一个单调性,可能为单调递增,也可能为单调递 ...

  5. poj 2559 Largest Rectangle in a Histogram 栈

    // poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...

  6. stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram

    题目传送门 /* 题意:宽度为1,高度不等,求最大矩形面积 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极 ...

  7. POJ 2559 Largest Rectangle in a Histogram -- 动态规划

    题目地址:http://poj.org/problem?id=2559 Description A histogram is a polygon composed of a sequence of r ...

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

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

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

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

随机推荐

  1. 3dContactPointAnnotationTool开发日志(三十)

      在vs2017里生成opencv时遇到了无法打开python27_d.lib的问题,具体解决请看这个,不过我用的是方法2,python37_d.lib找不到同理.   Windows下可以用的op ...

  2. 【.Net】net 反射15分钟速成

    概述 什么是反射 Reflection,中文翻译为反射.        这是.Net中获取运行时类型信息的方式,.Net的应用程序由几个部分:‘程序集(Assembly)’.‘模块(Module)’. ...

  3. HDU4701_Game

    很有意思,很好的一个题目. 题目的意思是两个人初始状态分别有A和B元,现在有N件可买的商品.两人轮流买,商品必须从左到右买过去,一次可以买若干个.第一个无法买到商品的人输. 一看就知道是博弈题目,但是 ...

  4. HDU4472_Count

    题目的意思简单,给你n个点,每个点(除根节点)都有且只有一个父节点.现在问你这些节点关系组成的图有多少种情况满足同一层节点上每个点的后继节点数量相等. 很简单,对于n节点,直接枚举n-1的的所有约数情 ...

  5. 用PHP写出显示客户端IP与服务器IP的代码

    打印客户端IP: echo $_SERVER[‘REMOTE_ADDR’]; 或者: getenv(‘REMOTE_ADDR’); 打印服务器IP: echo gethostbyname(“www.b ...

  6. [AT2364] [agc012_d] Colorful Balls

    题目链接 AtCoder:https://agc012.contest.atcoder.jp/tasks/agc012_d 洛谷:https://www.luogu.org/problemnew/sh ...

  7. 【BZOJ3244】【NOI2013】树的计数(神仙题)

    [BZOJ3244][NOI2013]树的计数(神仙题) 题面 BZOJ 这题有点假,\(bzoj\)上如果要交的话请输出\(ans-0.001,ans,ans+0.001\) 题解 数的形态和编号没 ...

  8. 【BZOJ5251】【八省联考2018】劈配(网络流,二分答案)

    [BZOJ5251][八省联考2018]劈配(网络流,二分答案) 题面 洛谷 BZOJ Description 一年一度的综艺节目<中国新代码>又开始了. Zayid从小就梦想成为一名程序 ...

  9. Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)

    A 每次可以换一个或不换,暴力枚举位置即可 B 模拟 C 二分答案.. 边界可以优化r=totb/(tota-p),二分可以直接(r-l>=EPS,EPS不要太小,合适就好),也可以直接限定二分 ...

  10. Insert Node in Sorted Linked List

    Insert a node in a sorted linked list. Have you met this question in a real interview?  Yes Example ...