hdu 1506 单调栈

Posted Aragaki

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 1506 单调栈相关的知识,希望对你有一定的参考价值。

#include <bits/stdc++.h>
#define PI acos(-1.0)
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
const double EPS = 1.0e-8;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
const int  maxn = 2e5 + 100;
const int  maxm = 300;
//next_permutation
//priority_queue<int, vector<int>, greater<int>> que;
ll h[100005];
ll l[100005], r[100005];
int main()
{
        //freopen("bonuses.in", "r", stdin);
        //freopen("out.txt", "w", stdout);
        ll ans = 0;
        int n;
        while (cin  >> n&&n)
        {
                mem(l,0);
                mem(r,0);
                ans=0;
                for (int i = 1; i <= n; i++)
                {
                        scanf("%lld", &h[i]);
                }
                l[1] = 1, r[n] = n;
                for (int i = 1; i <= n; i++)
                {
                        int cur = i;
                        while (cur != 1 && h[cur - 1] >= h[i])
                        {
                                cur = l[cur - 1];
                        }
                        l[i] = cur;
                }
                for (int i = n - 1; i >= 1; i--)
                {
                        int cur = i;
                        while (cur != n && h[cur + 1] >= h[i])
                        {
                                cur = r[cur + 1];
                        }
                        r[i] = cur;
                }
                for (int i = 1; i <= n; i++)
                {
                        ans = max(ans, 1ll * (r[i] - l[i] + 1) * h[i]);
                }
                cout << ans << endl;
        }
}

 

以上是关于hdu 1506 单调栈的主要内容,如果未能解决你的问题,请参考以下文章

HDU_1506_单调栈

hdu 1506 单调栈

Largest Rectangle in a Histogram HDU - 1506 (单调栈)

hdu_1506:Largest Rectangle in a Histogram 单调栈

HDU - 1506 Largest Rectangle in a Histogram (单调栈/笛卡尔树)

*HDU - 1506POJ - 2559Largest Rectangle in a Histogram(单调栈或动态规划)