单调栈--poj2559

poj2559
题意:求最大矩阵面积
单调栈板子

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <iomanip>
#include <cstdio>

using namespace std;
typedef long long LL;
typedef pair<double, double> PDD;
typedef pair<LL, LL> PLL;

const LL N = 1e6+50;
const LL MOD = 1e9+7;
const LL INF = 0x3f3f3f3f;

#define lson l, m, rt>>1
#define rson m+1, r, rt>>1|1

int n;
LL a[N], l[N], r[N];
int main()
{
    while(scanf("%d", &n) != EOF && n)
    {
        LL ans = 0;
        for(int i = 1;i <= n;++i) scanf("%lld", &a[i]);
        stack<LL> s;
        s.push(n+1);a[n+1] = -1;
        for(int i = n;i;--i)
        {
            while(a[s.top()] >= a[i])
                s.pop();
            r[i] = s.top();
            s.push(i);
        }
        while(!s.empty()) s.pop();
        s.push(0);a[0] = -1;
        for(int i = 1;i <= n;++i)
        {
            while(a[s.top()] >= a[i])
                s.pop();
            ans = max(ans, a[i]*(r[i]-s.top()-1));
            s.push(i);
        }
        cout << ans << endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/shuizhidao/p/11455118.html