题意:价值 = 区间和 × 区间最小值,求数组的子区间的最大价值
(1)区间和---->前缀和
(2)O(n^2) 枚举区间 ---> O( n ) 枚举元素,根据当前元素查询相应区间和
对每个元素,维护他作为最小值的左右端点,枚举数组中的元素,该元素大于0为例,查找( i , r [ i ] )的前缀和最大值,( l [ i ] - 1 , i - 1 )的前缀和最小值,注意这里 l [ i ] -1 可能会小于1 ,ST表应从0开始维护
丧心病狂的压行QWQ
ll a[MAXN], l[MAXN], r[MAXN];
ll sum[MAXN], mxv[MAXN][23], mnv[MAXN][23];
ll quemx(int l, int r)
{
int k = log2(r - l + 1);
return max(mxv[l][k], mxv[r - (1 << k) + 1][k]);
}
ll quemn(int l, int r)
{
int k = log2(r - l + 1);
return min(mnv[l][k], mnv[r - (1 << k) + 1][k]);
}
signed main()
{
int n; cin >> n;
rpp(i, n) cin >> a[i], l[i] = r[i] = i, sum[i] = sum[i - 1] + a[i], mxv[i][0] = mnv[i][0] = sum[i];
for(int i=1;i<=n;++i) while(l[i]>1&&a[i]<=a[l[i]-1]) l[i]=l[l[i]-1];
for(int i=n;i>=1;--i) while(r[i]<n&&a[i]<=a[r[i]+1]) r[i]=r[r[i]+1];
for (int j = 1; (1 << j) - 1 <= n; ++j)
for (int i = 0; i + (1 << j) -1 <= n; ++i)
{
mxv[i][j] = max(mxv[i][j - 1], mxv[i + (1 << (j - 1))][j - 1]);
mnv[i][j] = min(mnv[i][j - 1], mnv[i + (1 << (j - 1))][j - 1]);
}
ll ans = -1e17;
rpp(i, n)
{
if (a[i] > 0) ans = max(ans, a[i] * (quemx(i, r[i]) - quemn(l[i] - 1, i - 1)));
else if (a[i] < 0) ans = max(ans, a[i] * (quemn(i, r[i]) - quemx(l[i] - 1, i - 1)));
else if (a[i] == 0) ans = max(ans, 0ll);
}
cout << ans << endl;
return 0;
}
原文地址:https://www.cnblogs.com/Herlo/p/12205259.html
时间: 2024-10-05 14:39:24