您的位置:首页 > 其它

POJ 2559 Largest Rectangle in a Histogram(单调栈)

2014-05-25 22:27 501 查看
题目链接:POJ 2559 Largest Rectangle in a Histogram

#include <iostream>
#include <cstdio>
#include <stack>

using namespace std;

struct Node
{
    int l;
    long long h;
};

int main()
{
    int n;
    while(scanf("%d", &n), n)
    {
        long long h, k = 0, _max = 0;
        stack <Node> S;
        Node temp, tmp;
        for(int i = 0; i < n; i++)
        {
            scanf("%lld", &h);
            temp.l = i, temp.h = h;
            while(!S.empty() && S.top().h >= h)
            {
                tmp = S.top();
                S.pop();
                k = tmp.h * (i - tmp.l);
                temp.l = tmp.l;
                _max = max(_max, k);
            }
            S.push(temp);
        }
        while(!S.empty())
        {
            tmp = S.top();
            S.pop();
            k = tmp.h * (n - tmp.l);
            _max = max(_max, k);
        }
        printf("%lld\n", _max);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: