SP1805 HISTOGRA

  

一开始没有继承宽度疯狂wa

wa的代码:

#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
#define inf 0x3f3f3f3f
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
const int N=2e6+5;
int n;
ll h[N],ans;
stack<int>st;

int main()
{
    while(cin>>n)
    {
       if(n==0)break;
       ans=0;while(st.size())st.pop();

       rep(i,1,n)scanf("%lld",&h[i]),ans=max(ans,h[i]);
       h[n+1]=0;h[0]=0;

       rep(i,1,n+1)
       {
           while(st.size()&&h[st.top()]>h[i])
           ans=max(ans,h[st.top()]*(i-st.top())),st.pop();

           if(!st.size())ans=max(ans,h[i]*(i));
           else ans=max(ans,h[i]*(i-st.top()) );

           st.push(i);
       }
       cout<<ans<<endl;
    }
    return 0;
}
View Code

 单调栈维护宽度

#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
#define inf 0x3f3f3f3f
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
const int N=2e6+5;
int n;
ll ans,h[N];
struct node
{
    ll pos,w;
};
stack<node>st;

int main()
{
    while(cin>>n)
    {
        if(!n)break;
        while(st.size())st.pop();
        rep(i,1,n)scanf("%lld",&h[i]);
        h[++n]=ans=0;
        rep(i,1,n)
        {
            int d=0;
            while(st.size()&&h[st.top().pos]>h[i])
            {
                d+=st.top().w;
                ans=max(ans,h[st.top().pos]*d);
                st.pop();
            }
            st.push((node){i,d+1});
        }
        cout<<ans<<endl;
    }
    return 0;
}
View Code

单调栈维护向左和向右最长延续距离  

#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
#define inf 0x3f3f3f3f
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
const int N=2e6+5;
int n;
ll ans,h[N],L[N],R[N];

stack<int>st;

int main()
{
    while(cin>>n)
    {
        if(!n)break;
        while(st.size())st.pop();

        rep(i,1,n)scanf("%lld",&h[i]),L[i]=R[i]=i;

        rep(i,1,n)//维护一个递增的单调栈
        {
            while(st.size()&&h[st.top()]>=h[i])st.pop();

            if(!st.size())L[i]=1;
            else L[i]=st.top()+1;
            st.push(i);
        }
        while(st.size())st.pop();
        repp(i,n,1)
        {
            while(st.size()&&h[st.top()]>=h[i])st.pop();
            if(!st.size())R[i]=n;
            else R[i]=st.top()-1;
            st.push(i);
        }
        ll ans=0;
        rep(i,1,n)
        ans=max(ans,h[i]*(R[i]-L[i]+1));
        cout<<ans<<endl;
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/bxd123/p/11392954.html