码迷,mamicode.com
首页 > 其他好文 > 详细

Largest Rectangle in a Histogram

时间:2018-09-02 23:38:57      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:alt   eof   break   while   bsp   技术分享   targe   color   图片   

技术分享图片

ps:单调栈,注意红色部分的代码。

int n;

stack<P> s;

inline void upd(LL &x, LL y) { (x < y) && (x = y); }

int main()
{
    while(sc(n) != EOF && n) {

    while(!s.empty()) s.pop();

    LL ans = 0;
    Rep(i, 1, n) {
        int x;
        sc(x);
        if (s.empty()) s.push(P(x, i));
        else if (x > s.top().first) s.push(P(x, i));
        else if (x == s.top().first) {
            P res = P(x, s.top().second);
            s.pop();
            s.push(res);
        }
        else {
            int pos;
            while(s.top().first > x) {
                P tp = s.top();
                s.pop();
                pos = tp.second;
                upd(ans, 1ll * (i - tp.second) * tp.first);
                if (s.empty()) break;
            }
            s.push(P(x, pos));
        }
    }

    while(!s.empty()) {
        P tp = s.top();
        s.pop();
        upd(ans, 1ll * (n + 1 - tp.second) * tp.first);
    }
    pr(ans);
    }
    return 0;
}

 

Largest Rectangle in a Histogram

标签:alt   eof   break   while   bsp   技术分享   targe   color   图片   

原文地址:https://www.cnblogs.com/zgglj-com/p/9575586.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!