19_08_26 school training [Max]

The meaning of problems

求$max_{l \leq r}{\{min{\{a_l,a_{l+1},...,a_r\}}*(r-l+1)\}}$


 

Think

Divide and conquer, consider a contribution to range across a point.


 

Code

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long int ll;
 4 const int maxn=5E5+5;
 5 const int inf=1E9;
 6 int n;
 7 ll ans,a[maxn];
 8 inline ll max(ll x,ll y)
 9 {
10     return x>y?x:y;
11 }
12 inline ll min(ll x,ll y)
13 {
14     return x<y?x:y;
15 }
16 void solve(int l,int r)
17 {
18     if(l==r)
19     {
20         ans=max(ans,a[l]);
21         return;
22     }
23     int mid=(l+r)>>1;
24     ll nowL=a[mid],nowR=a[mid+1];
25     for(int i=mid+1,j=mid;i<=r;++i,nowR=min(nowR,a[i]))
26     {
27         while(j>=l&&nowL>=nowR)
28         {
29             --j;
30             nowL=min(nowL,a[j]);
31         }
32         ll len=i-j;
33         ans=max(ans,len*nowR);
34     }
35     nowL=a[mid],nowR=a[mid+1];
36     for(int i=mid,j=mid+1;i>=l;--i,nowL=min(nowL,a[i]))
37     {
38         while(j<=r&&nowL<=nowR)
39         {
40             ++j;
41             nowR=min(nowR,a[j]);
42         }
43         ll len=j-i;
44         ans=max(ans,len*nowL);
45     }
46     solve(l,mid),solve(mid+1,r);
47 }
48 int main()
49 {
50     ios::sync_with_stdio(false);
51     cin>>n;
52     for(int i=1;i<=n;++i)
53         cin>>a[i];
54     solve(1,n);
55     cout<<ans<<endl;
56     return 0;
57 }
View Code

 

Guess you like

Origin www.cnblogs.com/GreenDuck/p/11410290.html