小A的柱状图

小A的柱状图

https://ac.nowcoder.com/acm/problem/23619

单调栈+前缀和
单调栈维护区间的长度,然后用前缀和来计算最大值

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
typedef long long ll;


ll a[maxn],h[maxn];


int main()
{
    ll n,ans=0;
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        cin>>a[i];
        a[i]+=a[i-1];
    }
    for(int i=1; i<=n; i++)
        cin>>h[i];
    stack <int> s;
    s.push(0);
    for(int i=1; i<=n; i++)
    {
        while(h[i]<h[s.top()])
        {
            int t1=s.top();
            s.pop();
            int t2=s.top();
            ans=max(ans,h[t1]*(a[i-1]-a[t2]));
        }
        s.push(i);
    }
    cout<<ans<<endl;
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务