zoukankan      html  css  js  c++  java
  • 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);
    }
    
  • 相关阅读:
    php 接口类与抽象类的实际作用
    php中的implements 使用详解
    swoole两种运行模式BASE和PROCESS的区别
    Java多线程总结
    Shell WordCount:一行shell命令统计固定格式单词词频
    收藏大数据相关写的比较好的博客
    Hive-SQL查询连续活跃登陆的用户
    Mysql触发器
    Python json字符串和字典相互转换
    MySQL基础之实现累加值
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852195.html
Copyright © 2011-2022 走看看