CSP2013-12-3最大矩形

单调栈

#include 
#include "cstring"
#include "stack"
using namespace std;
struct t
{
    int w;
    int h;
};
struct t a[1005];
int main()
{
    int n;
    cin >> n;
    int max;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i].h;
        a[i].w = 1;
    }
        
    a[n].h = 0;
    stack b;
    max = a[0].h;
    
    for (int i = 0; i <= n; i++)
    {
        if (b.empty())
            b.push(a[i]);
        else if (a[i].h >= b.top().h)
            b.push(a[i]);
        else
        {
            int num = 0;
            while (!b.empty())
            {
                
                if(a[i].h >= b.top().h)break;
                int temp = b.top().h*(b.top().w+num);
                if (temp > max)
                    max = temp;
                num+=b.top().w;
                b.pop(); 
            }
            a[i].w+=num;
            b.push(a[i]);
        }
    }
    cout << max << endl;
    return 0;
}

 

你可能感兴趣的:(CSP)