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 \times 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
题意:给定n个数,求 区间最小值*区间和 的值最大
分析:这是一道区间的题目,这里可以归于RMQ问题,在RMQ问题当中有一个查询复杂度O(1)和建表复杂度O(nlogn)的数据结构--ST表;通过这个表就能够很快的解决查询的操作,相对于线段树更方便一些;
具体需要怎么做????
首先这题有一个需要处理的地方就是区间值有可能为负值,那么这样再去乘一个负值有可能结果就会很大,那么怎么去解决呢--通过ST表去维护一个前缀最小区间和与前缀最大区间和,他们之间的差值就是区间最值,是最大还是最小值,就需要看当前这个区间乘的值是正值还是负值了;
如果a[i]是正值,那么就是要用最大前缀和减去最小前缀和;反之,就是用前缀最小值减去前缀最大值;
每次通过这个最小值的下标去递归枚举这个a[i]能覆盖左右区间的答案(这个a[i]是最小值,其覆盖的左右区间的值应该都是大于这个a[i]的)
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <set>
#include <stack>
using namespace std;
typedef long long ll;
const int N = 5e5+100;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
const int MOD=1e9+7;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define Abs(x) ((x)>=0?(x):-(x))
ll dmin[N][20],dmax[N][20],a[N],sum[N],minid[N][20];
int n;
void RMQ_init(){
rep(i,1,n) dmin[i][0]=dmax[i][0]=sum[i],minid[i][0]=i;
// rep(i,0,n-1) num[i][0].val=a[i],num[i][0].id=i;
for(int j=1;(1<<j)<=n+1;j++)
for(int i=0;i+(1<<j)-1<=n;i++){
dmax[i][j]=max(dmax[i][j-1],dmax[i+(1<<(j-1))][j-1]);
dmin[i][j]=min(dmin[i][j-1],dmin[i+(1<<(j-1))][j-1]);
if(a[minid[i][j-1]]<a[minid[i+(1<<(j-1))][j-1]])
minid[i][j]=minid[i][j-1];
else
minid[i][j]=minid[i+(1<<(j-1))][j-1];
}
}
ll RMQ_minsum(int l,int r){
int k=0;
while((1<<(k+1))<=r-l+1) k++;
return min(dmin[l][k],dmin[r-(1<<k)+1][k]);
}
ll RMQ_maxsum(int l,int r){
int k=0;
while((1<<(k+1))<=r-l+1) k++;
return max(dmax[l][k],dmax[r-(1<<k)+1][k]);
}
ll RMQ_minval(int l,int r){
int k=0;
while((1<<(k+1))<=r-l+1) k++;
if(a[minid[l][k]]<a[minid[r-(1<<k)+1][k]])
return minid[l][k];
else
return minid[r-(1<<k)+1][k];
}
ll ans=-LINF;
void query(int l,int r){
if(l>r) return ;
int p=RMQ_minval(l,r);
if(a[p]>0)
ans=max((RMQ_maxsum(p,r)-RMQ_minsum(l-1,p-1))*a[p],ans);//这个地方是l-1,不是l
else
ans=max((RMQ_minsum(p,r)-RMQ_maxsum(l-1,p-1))*a[p],ans);
query(l,p-1);
query(p+1,r);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
scanf("%d",&n);
rep(i,1,n) scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i];
RMQ_init();
query(1,n);
printf("%lld\n",ans);
return 0;
}
上面获得这个最小值是通过ST表来获得的,其实还可以用单调栈去获得每一个a{i}能覆盖的左右区间
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <set>
#include <stack>
using namespace std;
typedef long long ll;
const int N = 5e5+100;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
const int MOD=1e9+7;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define Abs(x) ((x)>=0?(x):-(x))
ll dmin[N][20],dmax[N][20],a[N],sum[N];
int n;
ll L[N],R[N];
void RMQ_init(){
rep(i,1,n) dmin[i][0]=dmax[i][0]=sum[i];
for(int j=1;(1<<j)<=n+1;j++)
for(int i=0;i+(1<<j)-1<=n;i++){
dmax[i][j]=max(dmax[i][j-1],dmax[i+(1<<(j-1))][j-1]);
dmin[i][j]=min(dmin[i][j-1],dmin[i+(1<<(j-1))][j-1]);
}
}
ll RMQ_minsum(int l,int r){
int k=0;
while((1<<(k+1))<=r-l+1) k++;
return min(dmin[l][k],dmin[r-(1<<k)+1][k]);
}
ll RMQ_maxsum(int l,int r){
int k=0;
while((1<<(k+1))<=r-l+1) k++;
return max(dmax[l][k],dmax[r-(1<<k)+1][k]);
}
ll ans=-LINF;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
scanf("%d",&n);
rep(i,1,n) scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i];
stack<int>s;
RMQ_init();
for(int i=1;i<=n;i++){
while(!s.empty()&&a[i]<=a[s.top()])
s.pop();
if(s.empty()) L[i]=1;
else L[i]=s.top()+1;
s.push(i);
}
while(!s.empty()) s.pop();
for(int i=n;i>=1;i--){
while(!s.empty()&&a[i]<=a[s.top()])
s.pop();
if(s.empty()) R[i]=n;
else R[i]=s.top()-1;
s.push(i);
}
for(int i=1;i<=n;i++){
if(a[i]<=0){
ans=max(ans,(RMQ_minsum(i,R[i])-RMQ_maxsum(L[i]-1,i))*a[i]);
}
else if(a[i]>0){
ans=max(ans,(RMQ_maxsum(i,R[i])-RMQ_minsum(L[i]-1,i))*a[i]);
}
}
printf("%lld\n",ans);
return 0;
}