Largest Rectangle in a Histogram-------单调队列
2014-05-06 19:37
399 查看
此题也是一道典型的单调队列例题,此题也可以用dp,搜索做,搜索和dp会简单一些。
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 integersh1,...,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
Sample Output
Hint
Huge input, scanf is recommended.
Source
Ulm Local 2003
解题分析:
此题提议大概是求解矩形中面积最大的矩形面积。
输入为:
首先是输入N个用例,然后依次输入每组数据,对应的输出N个用例对应的最大面积。
做法:这里用的是单调队列求解。
做法是设两个数组一个存储原数组的元素,另一个是存储一个(i,j)这个范围内的数,
i-1一定要比i小,j+1一定也要小于等于j。可能有多个范围,要求出最大的范围,然后用范围j-i的值,就是长度,然后乘以高度。求出面积。
代码:
#include<stdio.h>
#include<algorithm>
#define MAX_N 100009
using namespace std;
int rect[MAX_N];
struct seg
{
long long value,s;
}q[MAX_N];
int main()
{
int n ;
while(scanf("%d",&n)&&n)
{
long long area = 0 ,top = 0;
for(int i=0;i!=n;++i)
scanf("%d",rect+i);
for(int i=0;i!=n;++i)
{
//队列为空或者当前元素大于队尾时入队
if(!top || q[top-1].value < rect[i])
{
q[top].value = rect[i];
q[top++].s = i ;
}
//此时,右边界出现,需要做更新
else if(q[top-1].value>rect[i])
{
//如果有比最大值大的面积,则更新最大值
while(top && q[top-1].value>rect[i])
{
--top;
area = max(area,q[top].value*(i-q[top].s));
}
//队空
if(!top)
q[top++].value = rect[i] ;
else if(q[top-1].value < rect[i])
q[top++].value = rect[i] ;
}
}
//队中可能还有元素,也要做相应的更新
while(top)
{
--top;
area = max(area,q[top].value*(n-q[top].s));
}
printf("%lld\n",area);
}
return 0;
}
Largest Rectangle in a Histogram
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13965 | Accepted: 4498 |
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 integersh1,...,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
Ulm Local 2003
解题分析:
此题提议大概是求解矩形中面积最大的矩形面积。
输入为:
首先是输入N个用例,然后依次输入每组数据,对应的输出N个用例对应的最大面积。
做法:这里用的是单调队列求解。
做法是设两个数组一个存储原数组的元素,另一个是存储一个(i,j)这个范围内的数,
i-1一定要比i小,j+1一定也要小于等于j。可能有多个范围,要求出最大的范围,然后用范围j-i的值,就是长度,然后乘以高度。求出面积。
代码:
#include<stdio.h>
#include<algorithm>
#define MAX_N 100009
using namespace std;
int rect[MAX_N];
struct seg
{
long long value,s;
}q[MAX_N];
int main()
{
int n ;
while(scanf("%d",&n)&&n)
{
long long area = 0 ,top = 0;
for(int i=0;i!=n;++i)
scanf("%d",rect+i);
for(int i=0;i!=n;++i)
{
//队列为空或者当前元素大于队尾时入队
if(!top || q[top-1].value < rect[i])
{
q[top].value = rect[i];
q[top++].s = i ;
}
//此时,右边界出现,需要做更新
else if(q[top-1].value>rect[i])
{
//如果有比最大值大的面积,则更新最大值
while(top && q[top-1].value>rect[i])
{
--top;
area = max(area,q[top].value*(i-q[top].s));
}
//队空
if(!top)
q[top++].value = rect[i] ;
else if(q[top-1].value < rect[i])
q[top++].value = rect[i] ;
}
}
//队中可能还有元素,也要做相应的更新
while(top)
{
--top;
area = max(area,q[top].value*(n-q[top].s));
}
printf("%lld\n",area);
}
return 0;
}
相关文章推荐
- hdu 1506 Largest Rectangle in a Histogram DP 单调队列
- Largest Rectangle in a Histogram--(单调队列orDP)
- 每日一dp(1)——Largest Rectangle in a Histogram(poj 2559)使用单调队列优化
- HDU 1506 Largest Rectangle in a Histogram(单调队列)
- HDU 1506 && POJ 2559 Largest Rectangle in a Histogram (单调队列)
- HDU OJ 1506 Largest Rectangle in a Histogram 和 NYOJ 258 最大长方形(二) 【单调队列】
- Largest Rectangle in a Histogram (单调队列)
- POJ2559 Largest Rectangle in a Histogram(比动态规划更快的方法——单调队列)
- HDU - 1506(单调队列) - Largest Rectangle in a Histogram
- Largest Rectangle in a Histogram (单调队列||dp)
- !HDU 1506 Largest Rectangle in a Histogram-dp|单调队列-(dp降低时间复杂度)
- 单调队列 POJ 2559 Largest Rectangle in a Histogram
- NYOJ-258/POJ-2559/HDU-1506 Largest Rectangle in a Histogram,最大长方形,dp或者单调队列!
- hdu 1506 Largest Rectangle in a Histogram 单调队列
- POJ 2559 Largest Rectangle in a Histogram 单调栈
- POJ 2559 Largest Rectangle in a Histogram RMQ || 单调栈
- E - Largest Rectangle in a Histogram ----单调栈
- Largest Rectangle in a Histogram POJ - 2559 单调栈
- 单调栈 poj2559 Largest Rectangle in a Histogram
- hdoj 1506 Largest Rectangle in a Histogram 【单调栈】