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
  • 相关阅读:
    Word快捷键大全
    IT人物TOP100英雄人物榜
    关于简历的理解
    opengl头文件:错误: 无法打开包括文件:“gl/glut.h”: No such file or directory
    关于两次算法竞赛的心得
    MFC中关于将控件与成员变量绑定,实现用子类重载控件
    怎么入门
    正则表达式
    每天更新,督促自己学习
    测试人员的出路
  • 原文地址:https://www.cnblogs.com/lglh/p/10747020.html
Copyright © 2011-2022 走看看