POJ2796 Feel Good
作者:互联网
题目
思路
2遍单调栈算贡献的左右边界,用前缀和预处理区间和,计算最优解。
code:
#include<iostream>
#include<stack>
#include<cstdio>
using namespace std;
stack<long long> u,u2;
long long n,a[100005],s[100005],l[100005],r[100005],ans,sl=1,sr=1,i;
int main()
{
scanf("%lld",&n);
for (i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
s[i]=s[i-1]+a[i];
}
for (i=1;i<=n;i++)
{
while (u.size()&&a[u.top()]>=a[i]) u.pop();
l[i]=(u.size()==0?1:u.top()+1);
u.push(i);
}
for (i=n;i>=1;i--)
{
while (u2.size()&&a[u2.top()]>=a[i]) u2.pop();
r[i]=(u2.size()==0?n:u2.top()-1);
u2.push(i);
if (ans<(s[r[i]]-s[l[i]-1])*a[i])
{
ans=(s[r[i]]-s[l[i]-1])*a[i];
sl=l[i],sr=r[i];
}
}
printf("%lld\n%lld %lld\n",ans,sl,sr);
return 0;
}
标签:Good,Feel,top,u2,100005,ans,size,POJ2796,lld 来源: https://blog.csdn.net/weixin_49843717/article/details/114433508