• 2018南昌邀请赛网络赛 单调栈 I


    Alice has a magic array. She suggests that the value of a interval is equal to the sum of the values in the interval, multiplied by the smallest value in the interval.

    Now she is planning to find the max value of the intervals in her array. Can you help her?

    Input

    First line contains an integer n(1 le n le 5 imes 10 ^5n(1≤n≤5×105).

    Second line contains nn integers represent the array a (-10^5 le a_i le 10^5)a(−105≤ai​≤105).

    Output

    One line contains an integer represent the answer of the array.

    样例输入复制

    5
    1 2 3 4 5

    样例输出复制

    36

    给出一个数列 请你选则任意一个区间,使区间中的最小值*区间的和最大

    首先对于一个5e5的数列 我们不可能把数列一一枚举

    所以我们采用单调栈 首先预处理出每个数字多管控的范围

    然后对于每个数字分正负讨论即可

    正数:所有数累加在一起

    负数:查一个区间使区间和最小即可(使用线段树)

    code:

    #include<bits/stdc++.h>
    using namespace std;
    int a[500005];
    int l[500005];
    int r[500005];
    stack<int> st;
    long long s[500005];
    long long MIN[500005*4],MAX[500005*4];
    void build(int root,int l,int r)
    {
        if(l==r)
        {
            MIN[root]=s[l];
            MAX[root]=s[r];
            return ;
        }
        int mid=(l+r)/2;
        build(root*2,l,mid);
        build(root*2+1,mid+1,r);
        MIN[root]=min(MIN[root*2],MIN[root*2+1]);
        MAX[root]=max(MAX[root*2],MAX[root*2+1]);
        return;
    }
    long long q(int root,int l,int r,int ll,int rr,int tp)
    {
        if(ll<=l&&r<=rr)
        {
            if(tp==0) return MIN[root];
            if(tp==1) return MAX[root];
        }
        int mid=(l+r)/2;
        if(rr<=mid) return q(root*2,l,mid,ll,rr,tp);
        else if(ll>mid) return q(root*2+1,mid+1,r,ll,rr,tp);
        else
        {
            if(tp==0) return min(q(root*2,l,mid,ll,rr,tp),q(root*2+1,mid+1,r,ll,rr,tp));
            if(tp==1) return max(q(root*2,l,mid,ll,rr,tp),q(root*2+1,mid+1,r,ll,rr,tp));
        }
    }
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=n; i>=1; i--)
        {
            while(st.size()&&a[st.top()]>=a[i])
            {
                st.pop();
            }
            if(st.size()==0)
            {
                r[i]=n;
            }
            else
            {
                r[i]=st.top()-1;
            }
            st.push(i);
        }
        while(st.size())
        {
            st.pop();
        };
        for(int i=1; i<=n; i++)
        {
            while(st.size()&&a[st.top()]>=a[i])
            {
                st.pop();
            }
            if(st.size()==0)
            {
                l[i]=1;
            }
            else
            {
                l[i]=st.top()+1;
            }
            st.push(i);
        }
        s[0]=0;
        for(int i=1; i<=n; i++)
        {
            s[i]=s[i-1]+a[i];
        }
        build(1,0,n);
        long long ans=-1e18;
        for(int i=1;i<=n;i++)
        {
            if(a[i]>=0)
            {
                ans=max(ans,a[i]*(s[r[i]]-s[l[i]-1]));
    //            printf("%d %d
    ",1,ans);
            }
            else
            {
                long long tMIN=q(1,0,n,i,r[i],0);
                long long tMAX=q(1,0,n,l[i]-1,i,1);
    //            cout<<tMIN<<" "<<tMAX<<endl;
                ans=max(ans,a[i]*(tMIN-tMAX));
            }
        }
        printf("%lld
    ",ans);
    }
    
  • 相关阅读:
    FreeSql.Repository (九)级联保存
    FreeSql.Repository (八)级联加载
    FreeSql.Repository (七)多表查询
    FreeSql.Repository (六)导航属性
    FreeSql.Repository (五)状态管理
    FreeSql.Repository (四)工作单元
    FreeSql.Repository (三)实体特性
    FreeSql.Repository (一)什么是仓储
    [开源] .Net 使用 ORM 访问 华为GaussDB数据库
    24位PCM采样数据转成16位算法,已实现PCM转WAV在线工具源码支持24bits、16bits、8bits
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852195.html
Copyright © 2020-2023  润新知