单调栈的问题
查看原帖
单调栈的问题
482728
Engulf楼主2023/10/20 13:14

参考题解https://www.luogu.com.cn/blog/wyy2020/solution-p4755

思路应该是,求出左边和右边最远的 ≤ai\le a_i 的数,可代码中为什么一个是 ≤\le 一个是 <<,没懂

for (int i = 1; i <= n; i++) {
  while (top && a[stk[top]] < a[i]) top--;
  if (top) lef[i] = stk[top] + 1;
  stk[++top] = i;
}
top = 0;
for (int i = n; i; i--) {
  while (top && a[stk[top]] <= a[i]) top--;
  if (top) rig[i] = stk[top] - 1;
  stk[++top] = i;
}
2023/10/20 13:14
加载中...