zoukankan      html  css  js  c++  java
  • poj2796(单调栈)

    传送门

    实测如果有多个值相等,输出最后一个最大值的LR

    ac代码:

    #include<bits/stdc++.h>
    #define per(i,a,b) for(int i=a;i<=b;i++)
    using namespace std;
    typedef long long ll;
    //#define int long long
    const ll inf =2333333333333333LL;
    const double eps=1e-8;
    int read(){
        char ch=getchar();
        int res=0,f=0;
        while(ch<'0' || ch>'9'){f=(ch=='-'?-1:1);ch=getchar();}
        while(ch>='0'&&ch<='9'){res=res*10+(ch-'0');ch=getchar();}
        return res*f;
    }
    // ------------------------head
    #define mod 1000000007
    const int N=100005;
    int n,L[N],R[N];
    ll s[N],a[N];
    stack<ll>st;
    
    signed main()
    {
        while(scanf("%d",&n)!=EOF){
            s[0]=0;
            while(!st.empty())st.pop();
            per(i,1,n){scanf("%lld",&a[i]);s[i]=s[i-1]+a[i];}
            for(int i=1;i<=n;i++){
                while(!st.empty()&&a[st.top()]>=a[i])st.pop();
                if(st.empty())L[i]=0;
                else L[i]=st.top();
                st.push(i);
            }
            while(!st.empty())st.pop();
            for(int i=n;i>=1;i--){
                while(!st.empty()&&a[st.top()]>=a[i])st.pop();
                if(st.empty())R[i]=n;
                else R[i]=st.top()-1;
                st.push(i);
            }
            ll res=0;
            int k;
            per(i,1,n){
                ll tmp=(ll)a[i]*(s[R[i]]-s[L[i]]);
                if(tmp>=res){
                    res=tmp;
                    k=i;
                }
            }
            printf("%lld
    %d %d
    ",res,L[k]+1,R[k]);
        }
        return 0;
    }
  • 相关阅读:
    UVa-272-TEX Quotes
    UVa-10881-蚂蚁
    UVa-1339-古老的密码
    POJ-1328-放置雷达
    POJ-3190-分配畜栏
    Openjudge-2787-算24
    WHYZOJ-#47. 滑行的窗口(单调队列)
    2017年9月16日18:03:54
    WHYZOJ-#93. 暗黑破坏神(垃圾01背包)
    WHYZOJ-#95 大逃亡(二分+BFS)(好题!!!)
  • 原文地址:https://www.cnblogs.com/WindFreedom/p/9674538.html
Copyright © 2011-2022 走看看