FJUT ACM 1899 Largest Rectangle in a Histogram

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
/**
【思路】:其实一开始维护一个单调的栈,
这个栈存储序号,然后判断是不是可以填入,
如果可以填进去,就是维护一个单调递增的栈
如果输入一个不能维护的话,就倒回去到比它小的
这样的话每次倒回去,就等于枚举了,枚举math[temp]*(i-stk.top()-1)
就是枚举一个面积,如果为空的话,就是这个放入的值是最小的
就进行维护,就是他只只能选择math[temp]*i
就是底下的长度。最后一个0保证了会去进行求面积操作。
这样的话复杂度就是O(N)
**/
ll math[100005];
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        ll s;
        s=0;
        memset(math,0,sizeof(math));
        for(int i=0; i<n; i++)
            scanf("%lld",&math[i]);
        math[n]=0;
        stack<int>stk;
        for(int i=0; i<=n; i++)
        {
            if(stk.empty()||math[stk.top()]<math[i])
                stk.push(i);
            else
            {
                int temp=stk.top();
                stk.pop();
                s=max(s,math[temp]*(stk.empty()?i:(i-stk.top()-1)));
                i--;
            }
        }
        while(!stk.empty())
            stk.pop();
        printf("%lld\n",s);
    }
    return 0;
}

 

posted @ 2018-03-19 22:03  moxin0509  阅读(170)  评论(0编辑  收藏  举报