zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    数据结构>图的应用(拓扑排序,关键路径) 小强斋
    数据结构>图的最短路径 小强斋
    数据结构>图的连通性和最小生成树 小强斋
    mysql查看表的存储引擎等相关信息
    java.util.TaskQueue的最小堆排序算法的应用
    jetty源码阅读总结1
    lucene3.6.0的高亮显示
    log4j MDC NDC应用场景
    velocity自定义标签和指令
    jetty关于http状态码处理的一个很有用的类
  • 原文地址:https://www.cnblogs.com/bxd123/p/11392954.html
Copyright © 2011-2022 走看看