*HDU - 1506POJ - 2559Largest Rectangle in a Histogram(单调栈或动态规划)

Posted wx62a8776062513

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了*HDU - 1506POJ - 2559Largest 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: 

*【HDU

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

 

解题报告:

 

    单调栈模板!此题用动态规划亦可解。题目大意就是求矩形的可最大面积

ac代码1:(因为第一次用单调栈所以代码十分丑陋、、、)

#include<bits/stdc++.h>

#define ll long long

using namespace std;
const int MAX = 100000 + 5 ;
const int INF = 0x3f3f3f3f;
int top;
stack <ll> sk,sk2;
ll a[MAX];
ll zuo[MAX],you[MAX];
ll maxx;
int main()

int n;
while(scanf("%d",&n) )
if( n==0 ) break;
maxx=0;
while(!sk.empty() ) sk.pop();
while(!sk2.empty() ) sk2.pop();
top = -1;
for(int i = 0; i<n; i++)
scanf("%lld",&a[i]);

//递增栈 找左侧的最小元素
sk.push(0);zuo[0]=-1;
for(int i = 1; i<n; i++)
if(sk.empty() )
zuo[i]=-1;
sk.push(i);

else
while(!sk.empty() && a[sk.top()]>=a[i] )
sk.pop();

if(sk.empty() )
zuo[i]=-1;
sk.push(i);

else
zuo[i]=sk.top();
sk.push(i);





sk2.push(n-1);you[n-1]=n;
for(int i = n-2; i>=0; i--)
if(sk2.empty() )
you[i]=n;
sk2.push(i);

else
while(!sk2.empty() && a[sk2.top()]>=a[i] )
sk2.pop();

if(sk2.empty() )
you[i]=n;
sk2.push(i);

else
you[i]=sk2.top();
sk2.push(i);




for(int i = 0; i<n; i++)
maxx=max(maxx,a[i]*(you[i]-zuo[i]-1) );

printf("%lld\\n",maxx);


return 0 ;

ac代码2:(动态规划)

 

如果确定了长方形的左端点L和右端点R,那么最大可能的高度就是minhi|L <= i < R。

L[i] = (j <= i并且h[j-1] < h[i]的最大的j)

R[i] = (j > i并且h[j] > h[i]的最小的j)

#include <stdio.h>

#define MAX_N 100000

int n;
int h[MAX_N];
int L[MAX_N], R[MAX_N];
int stack[MAX_N];

long long max(long long a, long long b)
return (a > b) ? a : b;


void solve()
//计算L
long long ans = 0;
int t = 0;
int i;
for (i = 0; i < n; ++i)
while (t > 0 && h[stack[t-1]] >= h[i]) t--;
L[i] = (t == 0) ? 0 : (stack[t-1] + 1);
stack[t++] = i;


//计算R
t = 0;
for (i = n - POJ 2559 单调栈

POJ2559 单调栈

[POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)

poj 2559(栈的应用)

[POJ 2559] 最大矩形面积

POJ 2559 - Largest Rectangle in a Histogram - [单调栈]