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
  • 相关阅读:
    点击滚动到任意位置
    设置滚动条
    突破浏览器限制字体大小不能低于12px的限制
    font-family 字体及各大主流网站对比
    可视区尺寸改变的时候,重绘Echarts
    <input />
    switch (xx) { case xx: break ......}
    让页面随浏览器的窗口大小缩放而缩放
    对后端返回的 html 标签不识别的解决办法
    html 标签分类:块级元素、内联元素 ......
  • 原文地址:https://www.cnblogs.com/bxd123/p/11392954.html
Copyright © 2011-2022 走看看