Largest Rectangle in a Histogram(POJ-2559)

Largest Rectangle in a Histogram
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22186   Accepted: 7178

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.

Source


題意理解:找出這裏面面積最大的矩形。

解題思路:這就是一道單調棧的題,找出每個端點能到達的左右邊界。就可以了。

首先確定左邊界 若棧爲空,則壓入東西,若不是,則比較棧頂元素的值和要壓入的值得大小關係,比棧頂大,壓入,比棧頂小,把棧頂元素彈出,並更新要壓入元素左邊界,直到遇到比要壓入元素小的值,或者棧爲空時,壓入該元素。

#include<stdio.h>
#include<stack>
using namespace std;
struct s{
   long long  x;
   long long l;
   long long  r;
}a[100010];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            return 0;
        for(int i=0;i<n;i++)
        {
            scanf("%lld",&a[i].x);
            a[i].l=i;
            a[i].r=i;
        }
        stack<s>q;
        for(int i=0;i<n;i++)   //確定左端點
        {
            if(q.empty())
            {
                q.push(a[i]);
            }
            else
            {
                if(q.top().x<a[i].x)
                {
                    q.push(a[i]);
                }
                else
                {
                    while(q.top().x>=a[i].x)
                    {
                        a[i].l=q.top().l;
                        q.pop();
                        if(q.empty())
                        {
                            break;
                        }
                    }
                   q.push(a[i]);
                }
            }
        }
        while(!q.empty())
        {
            q.pop();
        }
        for(int i=n-1;i>=0;i--)    //確定右端點
        {
            if(q.empty())
            {
                q.push(a[i]);
            }
            else
            {
                if(q.top().x<a[i].x)
                {
                    q.push(a[i]);
                }
                else
                {
                    while(q.top().x>=a[i].x)
                    {
                        a[i].r=q.top().r;
                        q.pop();
                        if(q.empty())
                        {
                            break;
                        }
                    }
                    q.push(a[i]);
                }
            }
        }
        long long ans;
        long long max_=0;
        for(int i=0;i<n;i++)
        {
            ans=(long long)((a[i].r-a[i].l+1)*a[i].x);
            if(max_<ans)
            {
                max_=ans;
            }
        }
        printf("%lld\n",max_);
    }
}


發佈了44 篇原創文章 · 獲贊 37 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章