1. 程式人生 > >POJ2559 Largest Rectangle in a Histogram

POJ2559 Largest Rectangle in a Histogram

Largest Rectangle in a Histogram

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 25861   Accepted: 8377

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.

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.

題意:給出一個圖有幾個矩形組成,矩形的寬都是一,高不定,計算幾個連續的矩形包含的最大面積。

思路:這個題是個單調棧,我們每次列舉每個矩形向左和向右延伸的最大程度,最大程度的延伸能夠保持面積的最大,例如

8 10 2,我們依次放出單調棧,8,10,當10放進去時,我們發現8向左不能延伸,向右可以延伸1位(10裡邊可以放的下),當2放進去時10向左不可以延伸,向右也不可以延伸,我們當做向右包括他自己算一位。2的話可以向左延伸,不可以向右延伸,並且我們在出現退棧的時候才計算,單調棧要保持單調性,所以要依次退棧,這樣我們就可以在加入2的時候把8,10退棧的時候以此計算8和10的向左向右延展程度。

我開始自己寫的那個程式碼有點醜,就是兩個for依次算出向左還有向右的值。看了大佬部落格發現一個for就可以了

大佬部落格:https://www.cnblogs.com/WABoss/p/5225464.html         

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
int a[100005];
int sta[100005];//陣列模擬單調棧
int r[100005];//向右看
int l[100005];//向左看
int main()
{
    int n,top;
    while(~scanf("%d",&n)&&n)
    {
        memset(a,0,sizeof(a));
        memset(sta,0,sizeof(sta));
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        top=0;
        a[++n]=-1;//要把最後一位變成-1,這樣可以保證都是遞增序列不出現退棧時也可以計算
        for(int i=1; i<=n; i++)
        {
            l[i]=i;
            r[i]=i;
            while(top&&a[sta[top]]>a[i])//當出現退棧的時候,a[i]都比top的小,那麼top向右不能延伸,自己就可以算一次,而a[i]就可以向左延伸
            {
                r[sta[top]]=i-1;//記錄此時向右的延伸到哪
                l[i]=l[sta[top]];//記錄此時向左的延伸到哪
                top--;
            }
            if(top&&a[sta[top]]==a[i])//相同時就直接明瞭,a[i]向左延伸就好了
            {
                l[i]=l[sta[top]];
            }
            sta[++top]=i;
        }
        ll ans=0;
        for(int i=1; i<n; i++)
        {
            ans=max(ans,(ll)(r[i]-l[i]+1)*a[i]);
//            printf("%d %d %d\n",r[i],l[i],ans);
        }
        printf("%lld\n",ans);
    }
}