hdu 1506 Largest Rectangle in a Histogram 构造

题目链接:HDU - 1506

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.

题意描述:给出一些宽度为1,高度大于等于0的矩形,求出最大的矩形面积。

算法分析:看到n的范围,就确定不能n*n了,想了想,对于一个矩形i ,找到最左方和最右连续都比它高的位置之后,对于矩形i+1来说,如果i+1高度比i小,那么对i+1来往左扩展找出左方连续比它高的最左方的位置时候,就没有必要再次比较i+1和i、i+1和i-1、、、因为前面有一部分由 i 已经比较过了,所以我们只需要从上次的记录的位置开始往左比较即可。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define inf 0x7fffffff
 8 using namespace std;
 9 typedef long long LL;
10 const int maxn=100000+10;
11
12 int n;
13 int l[maxn],r[maxn],an[maxn];
14
15 int main()
16 {
17     while (scanf("%d",&n)!=EOF && n)
18     {
19         for (int i=1 ;i<=n ;i++) {scanf("%d",&an[i]);l[i]=r[i]=i;}
20         an[0]=an[n+1]=-1;
21         l[0]=l[n+1]=r[0]=r[n+1]=0;
22         for (int i=1 ;i<=n ;i++)
23         {
24             while (an[l[i]-1 ]>=an[i])
25                 l[i]=l[l[i]-1 ];
26         }
27         for (int i=n ;i>=1 ;i--)
28         {
29             while (an[r[i]+1 ]>=an[i])
30                 r[i]=r[r[i]+1 ];
31         }
32         LL ans=0;
33         for (int i=1 ;i<=n ;i++)
34         {
35             LL area=(LL)(r[i]-l[i]+1)*(LL)an[i];
36             if (area>ans) ans=area;
37         }
38         printf("%I64d\n",ans);
39     }
40     return 0;
41 }
时间: 2024-10-11 12:52:39

hdu 1506 Largest Rectangle in a Histogram 构造的相关文章

HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1506 Appoint description: Description A histogram is a polygon composed of a sequence of rectangles aligned a

HDU 1506 Largest Rectangle in a Histogram

Largest Rectangle in a Histogram Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 150664-bit integer IO format: %I64d      Java class name: Main A histogram is a polygon composed of a sequence of rectangles al

hdu 1506 Largest Rectangle in a Histogram 单调队列

Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12554    Accepted Submission(s): 3509 Problem Description A histogram is a polygon composed of a sequence of rec

HDU -1506 Largest Rectangle in a Histogram&amp;&amp;51nod 1158 全是1的最大子矩阵 (单调栈)

单调栈和队列讲解:传送门 HDU -1506题意: 就是给你一些矩形的高度,让你统计由这些矩形构成的那个矩形面积最大 如上图所示,如果题目给出的全部是递增的,那么就可以用贪心来解决 从左向右依次让每一个矩形的高度当作最后的高度,来从中选取最大值就可以了 但是如果它不是递增的,中间会出现低谷,那么要还想运用贪心策略就要把之前高度大于它的全部扔掉,但是再扔掉他们之前还要判断一下以他们为最后答案的高度可不可行,这样我们就是在构造一个递增序列,可以用栈来维护它 代码: 1 #include<stdio.

hdu 1506 Largest Rectangle in a Histogram 单调栈

#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int a[100100],q[100100],l[100100],r[100100]; int main() { int i,n,cnt; while(scanf("%d",&n),n!=0) { for(i=1;i<=n;i++) { scanf("%d",

hdu 1506 Largest Rectangle in a Histogram(DP)

题意: 有一个柱状图,有N条柱子.每一条柱子宽度都为1,长度为h1...hN. 在这N条柱子所构成的区域中找到一个最大面积,每平方米3块钱,问最多赚多少钱. 输入: 1<=N<=100000 0<=hi<=1000000000 思路: N很大,肯定得用一个O(N)或O(NLOGN)的算法,, 假如这个面积的长是从第i条柱子到第j条柱子,宽则一定是第i条柱子到第j条柱子中最矮的那条柱子的高度. 而我们站在那根最矮的柱子向左看,第i-1条的柱子一定是小于当前柱子的,否则可以加进去.向右

hdu 1506 Largest Rectangle in a Histogram ((dp求最大子矩阵))

# include <stdio.h> # include <algorithm> # include <iostream> # include <math.h> using namespace std; __int64 a[100010],l[100010],r[100010];///l[i]左边连续大于等于a[i]的下标,r[i]右边连续大于等于a[i]的下标,所以对于a[i]的矩形面积为(l[i]-r[i]+1)*a[i]; int main() {

HDU - 1506 Largest Rectangle in a Histogram(dp)

题意:已知n个高度不一.宽度相同的矩形并列排放,求所形成的图形中最大的矩形面积. 分析: 1.对于每一个矩形,分别算出它左边连续比它高的矩形中最左边的下标,右边同理.通过(r[i] - l[i] + 1) * a[i]比较得到最大的矩形面积. 2.将一组高度依次降低的矩形看成一个整体,如果该矩形比这个整体最矮的矩形都矮,长度可直接延伸过去,通过tmp = l[tmp - 1],依次向左比较,直到找到最左边的下标. #include<cstdio> #include<cstring>

hdu 1506 Largest Rectangle in a Histogram(求最大的矩形)

1.注意要把a[]定义为LL,我在这里wa了N次 2.寻找边界时,得用dp思想 AC代码: #include<cstdio> #include<cstring> #define LL long long using namespace std; const LL INF=1<<60; LL a[100005];//要定义为LL int L[100005]; int R[100005]; int main() { int n; while(scanf("%d&q