但使书种多,会有岁稔时。这篇文章主要讲述*HDU - 1506POJ - 2559Largest Rectangle in a Histogram(单调栈或动态规划)相关的知识,希望能为你提供帮助。
题干:
Description
【*HDU - 1506POJ - 2559Largest Rectangle in a Histogram(单调栈或动态规划)】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
解题报告:
单调栈模板!此题用动态规划亦可解。题目大意就是求矩形的可最大面积
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 - 3250 Bad Hair Day (单调栈)
- CF#459 A Pashmak and Garden(水题)
- HDU - 1870愚人节的礼物(水题模拟 思想类似于栈())
- CF#-931A Friends Meeting(思维)
- 什么是真正的敏捷开发(敏捷开发与瀑布开发有何不同)
- CF#468 div2 D. Peculiar apple-tree(思维)
- HDU - 1301Jungle Roads(并查集+最小生成树)(内附最小生成树两种算法 克鲁斯特尔算法&&普里姆算法)
- client-go gin的简单整合七-继续完善
- 全链路监控pinpoint安装部署