1. 程式人生 > >[洛谷P2422]良好的感覺[單調佇列]

[洛谷P2422]良好的感覺[單調佇列]

開始寫了個隊頭隊尾都pop的假做法...還搞到了70pts 後來拍了拍發現自己傻了


int n, a[MAXN], l[MAXN], r[MAXN], sta[MAXN], top;//l[i], r[i]是a[i]作為最小值的最左和最右端點
ll pre[MAXN];
int main() {
  ll ans = 0;
  in, n;
  lop1(i, n) in, a[i];
  a[++n] = 0;
  lop1(i, n) pre[i] = pre[i - 1] + a[i];    
  lop1(i, n) l[i] = i;
  lop1(i, n) {
    while (top && a[sta[top]] >= a[i]) r[sta[top]] = i - 1, chmin(l[i], l[sta[top--]]);
    sta[++top] = i;
  }
  lop1(i, n) chmax(ans, 1ll * (pre[r[i]] - pre[l[i]-1]) * a[i]);
  out, ans, '\n';
  return 0;
}