zoukankan      html  css  js  c++  java
  • Max answer(单调栈+ST表)

    Max answer 

    https://nanti.jisuanke.com/t/38228

    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(1n5×105).

    Second line contains nn integers represent the array a (-10^5 le a_i le 10^5)a(105ai105).

    Output

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

    样例输入

    5
    1 2 3 4 5

    样例输出

    36

    题意:给定n个数,求 区间最小值*区间和 的值最大

    思路:先求前缀和,然后用st表求出区间最大最小值,再用单调栈求出每个数的左右边界,然后枚举最小值和它的区间和即可

     1 #include<bits/stdc++.h>
     2 typedef long long ll;
     3 using namespace std;
     4 #define maxn 500005
     5 int n;
     6 ll sum[maxn],R[maxn],L[maxn],a[maxn],Max[maxn][25],Min[maxn][25];
     7 
     8 ll queryMax(int x,int y){
     9     int k=log2(y-x+1);
    10     return max(Max[x][k],Max[y-(1<<k)+1][k]);
    11 }
    12 
    13 ll queryMin(int x,int y){
    14     int k=log2(y-x+1);
    15     return min(Min[x][k],Min[y-(1<<k)+1][k]);
    16 }
    17 
    18 int main(){
    19     scanf("%d",&n);
    20     for(int i=1;i<=n;i++){
    21         scanf("%lld",&a[i]);
    22         sum[i]=sum[i-1]+a[i];
    23         Max[i][0]=sum[i];
    24         Min[i][0]=sum[i];
    25     }
    26     for(int i=1;i<25;i++){
    27         for(int j=0;j+(1<<i)-1<=n;j++){
    28             Max[j][i]=max(Max[j][i-1],Max[j+(1<<(i-1))][i-1]);
    29             Min[j][i]=min(Min[j][i-1],Min[j+(1<<(i-1))][i-1]);
    30         }
    31     }
    32 
    33     stack<int>st;
    34     for(int i=1;i<=n;i++){
    35         while(!st.empty()&&a[i]<=a[st.top()]){
    36             st.pop();
    37         }
    38         if(st.empty()){
    39             L[i]=1;
    40         }
    41         else{
    42             L[i]=st.top()+1;
    43         }
    44         st.push(i);
    45     }
    46     while(!st.empty()) st.pop();
    47     for(int i=n;i>=1;i--){
    48         while(!st.empty()&&a[i]<=a[st.top()]){
    49             st.pop();
    50         }
    51         if(st.empty()){
    52             R[i]=n;
    53         }
    54         else{
    55             R[i]=st.top()-1;
    56         }
    57         st.push(i);
    58     }
    59     ll ans=-0x3f3f3f3f3f3f3f3f;
    60     for(int i=1;i<=n;i++){
    61         if(a[i]<0){
    62             ll minr=queryMin(i,R[i]);
    63             ll maxl=queryMax(L[i]-1,i);
    64             ans=max(ans,(minr-maxl)*a[i]);
    65         }
    66         else if(a[i]>0){
    67             ll maxr=queryMax(i,R[i]);
    68             ll minl=queryMin(L[i]-1,i);
    69             ans=max(ans,(maxr-minl)*a[i]);
    70         }
    71         else{
    72             ans=max(ans,0LL);
    73         }
    74     }
    75     printf("%lld
    ",ans);
    76 }
    View Code
  • 相关阅读:
    ES6函数剩余参数(Rest Parameters)
    ES6函数默认参数(Default Parameters)
    Mac os下换行符导致发布到npm里的命令行模块不能使用问题
    Mac OSX下卸载Nodejs
    ES6箭头函数(Arrow Functions)
    IE6-9不支持Textarea的maxlength属性
    日期相关的小函数汇总
    IE6-11使用location.href提交时的链接复制到firefox或chrome时出现乱码
    表驱动法
    PlaceHolder的两种实现方式
  • 原文地址:https://www.cnblogs.com/Fighting-sh/p/10755910.html
Copyright © 2011-2022 走看看