zoukankan      html  css  js  c++  java
  • Max answer(The Preliminary Contest for ICPC China Nanchang National Invitational)

    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(1n5×10^5).

    Second line contains nn integers represent the array a(10^5ai10^5).

    Output

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

    样例输入

    5
    1 2 3 4 5

    样例输出

    36
    题解:
    枚举每个元素作为最小值时的最优答案,然后取最大值。首先根据单调队列的思想,求出每个元素作为最小值时能够管辖的范围。
    如果这个元素是正数或零,那么就是它所管辖的区间的值的和乘上该元素,因为他是正数,所以它所管辖的区间内的值均为正数。
    如果这个元素是负数,那我们只需要找到
    从它到它能影响到的范围的最右边前缀和的最小值-从它左边第一个元素到它能影响到的最左边的左边第一个元素这个区间前缀和的最大值,然后乘上该元素。
    AC代码:
      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 typedef long long ll;
      4 const int maxn=5e5+50;
      5 int n,ri[maxn],le[maxn],st[maxn];
      6 ll ans=-1e18,a[maxn],sum[maxn],mi[maxn*4],ma[maxn*4];
      7 void build(int L,int R,int rt)
      8 {
      9     if(L==R)
     10     {
     11         mi[rt]=ma[rt]=sum[L];
     12         return ;
     13     }
     14     int mid=(R+L)/2;
     15     build(L,mid,2*rt);
     16     build(mid+1,R,2*rt+1);
     17     mi[rt]=min(mi[2*rt],mi[2*rt+1]);
     18     ma[rt]=max(ma[2*rt],ma[2*rt+1]);
     19 }
     20 ll qmi(int L,int R,int rt,int l,int r)
     21 {
     22     if(l<=L && R<=r) return mi[rt];
     23     int mid=(L+R)/2;
     24     ll ret=1e18;
     25     if(l<=mid) ret=min(ret,qmi(L,mid,2*rt,l,r));
     26     if(r>=mid+1) ret=min(ret,qmi(mid+1,R,2*rt+1,l,r));
     27     return ret;
     28 }
     29 ll qma(int L,int R,int rt,int l,int r)
     30 {
     31     if(l<=L && R<=r) return ma[rt];
     32     int mid=(L+R)/2;
     33     ll ret=-1e18;
     34     if(l<=mid) ret=max(ret,qma(L,mid,2*rt,l,r));
     35     if(r>=mid+1) ret=max(ret,qma(mid+1,R,2*rt+1,l,r));
     36     return ret;
     37 }
     38 int main()
     39 {
     40     scanf("%d",&n);
     41     for(int i=1;i<=n;i++)
     42     {
     43         scanf("%lld",&a[i]);
     44         sum[i]=sum[i-1]+a[i];
     45     }
     46     build(1,n,1);
     47     for(int i=1;i<=n;i++)
     48     {
     49         int cnt=i-1;
     50         while(cnt>=1 && a[cnt]>=a[i])
     51         {
     52             cnt=le[cnt];
     53         }
     54         le[i]=cnt;
     55     }
     56     for(int i=1;i<=n;i++) le[i]++;
     57     for(int i=n;i>=1;i--)
     58     {
     59         int cnt=i+1;
     60         while(cnt<=n && a[cnt]>=a[i])
     61         {
     62             cnt=ri[cnt];
     63         }
     64         ri[i]=cnt;
     65     }
     66     for(int i=1;i<=n;i++) ri[i]--;
     67     /*int top=0;
     68     for(int i=1;i<=n;i++)
     69     {
     70         while(top && a[st[top]]>a[i])
     71         {
     72             ri[st[top]]=i-1;
     73             top--;
     74         }
     75         st[++top]=i;
     76     }
     77     while(top) ri[st[top--]]=n;
     78     for(int i=n;i>=1;i--)
     79     {
     80         while(top && a[st[top]]>a[i])
     81         {
     82             le[st[top]]=i+1;
     83             top--;
     84         }
     85         st[++top]=i;
     86     }
     87     while(top) le[st[top--]]=1;*/
     88     for(int i=1;i<=n;i++)
     89     {
     90         if(a[i]>=0) ans=max(ans,a[i]*(sum[ri[i]]-sum[le[i]-1]));
     91         else
     92         {
     93             if(le[i]==1)
     94             {
     95                 if(i==1) ans=max(ans,a[i]*a[i]);
     96                 else ans=max(ans,a[i]*(qmi(1,n,1,i,ri[i])-max(0ll,qma(1,n,1,le[i],i-1))));
     97             }
     98             else
     99             {
    100                ans=max(ans,a[i]*(qmi(1,n,1,i,ri[i])-qma(1,n,1,le[i]-1,i-1)));
    101             }
    102         }
    103     }
    104     printf("%lld
    ",ans);
    105     return 0;
    106 }
    View Code
  • 相关阅读:
    Java通过socket和DTU,RTU连接工业传感器通信
    没有远程桌面授权服务器可以提供许可证 相关,注册表项获得权限方法
    freertos内存pvPortMalloc 和 malloc 区别 ,以及全局变量占用情况
    自学it吧论坛
    安全学习地址
    服务器zfs文件系统修复过程记录
    第1年10月11日 android canvas
    第1年10月9日 swiftui
    第1年9月28日 Android 4.4以后,浏览器内核采用的chromium,可以支持webrtc
    第1年9月19日 ios librtmp
  • 原文地址:https://www.cnblogs.com/lglh/p/10747020.html
Copyright © 2011-2022 走看看