原题链接:POJ 2796 Feel Good
题意简述:
求某段区间的最小值*这段区间所有元素之和的最大值;
代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<cstdio> using namespace std; const int N = 100010; typedef long long ll; ll a[N], st[N], sum[N]; int n, top = 0; ll ans = -1, l, r; int main(){ scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i]; //前缀和预处理 a[n + 1] = -1; for(int i = 1; i <= n + 1; i++){ while(top && a[st[top]] > a[i]){ ll temp = (ll)a[st[top]] * (sum[i - 1] - sum[st[top - 1]]); if(ans < temp){ ans = temp; l = st[top - 1] + 1; r = i - 1; } --top; } ++top; st[top] = i; } printf("%lld %lld %lld ", ans, l, r); return 0; }