POJ - 2559 Largest Rectangle in a Histogram 单调栈

单调栈的练手题,主要内容就是把每一个矩形右界限和左界限算出来比较大小就OK了。
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

int l[100005],r[100005],h1[100005];
long long max1,p;

int main()
{
    int n;
    while (scanf("%d",&n)!=EOF&&n)
    {
        for (int i=1; i<=n; i++)
        {
            scanf("%d",&h1[i]);
        }
        h1[0]=-1;
        for (int i=1; i<=n; i++)
        {
            int j=i-1;
            while (h1[i]<=h1[j]) j=l[j];
            l[i]=j;
        }
        h1[n+1]=-1;
        for (int i=n; i>0; i--)
        {
            int j=i+1;
            while (h1[i]<=h1[j]) j=r[j];
            r[i]=j;
        }
        max1=0;
        for (int i=1; i<=n; i++)
        {
            p=(long long)(r[i]-l[i]-1)*h1[i];
            max1=p>max1?p:max1;
        }
        cout<<max1<<endl;
    }
    return 0;
}


你可能感兴趣的:(POJ - 2559 Largest Rectangle in a Histogram 单调栈)