题目大意:
然后就是求区间和区间中最小值的乘积的最大,输出最大值并输出左右边界;
基本思路:
单调栈;
代码如下:
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<list>
#include<stack>
#include<map>
#include<set>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 100000+10;
typedef long long int ll;
int l[maxn],r[maxn];
ll ans[maxn];
ll sum[maxn];
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%I64d",&ans[i]);
sum[i]=sum[i-1]+ans[i];
}
stack<int>s1,s2;
for(int i=1;i<=n;i++)
{
while(!s1.empty()&&ans[s1.top()]>=ans[i]) s1.pop();
if(s1.empty()) l[i]=1;
else l[i]=s1.top()+1;
s1.push(i);
}
for(int i=n;i>=1;i--)
{
while(!s2.empty()&&ans[s2.top()]>=ans[i]) s2.pop();
if(s2.empty()) r[i]=n;
else r[i]=s2.top()-1;
s2.push(i);
}
ll maxx=-inf,t;
int left,right;
for(int i=1;i<=n;i++)
{
t=ans[i]*(sum[r[i]]-sum[l[i]]+ans[l[i]]);
if(t>maxx)
{
maxx=t;
left=l[i],right=r[i];
}
}
printf("%I64d\n%d %d\n",maxx,left,right);
return 0;
}