42. 接雨水
方法一:单调栈!!!!在这个题复习一下
单调栈最常见的应用场景:适合求一个序列中的每个数 左边或者右边第一个比当前元素大或者小的元素。(找到他的数值/对应下标。)
再刷一遍acw单调栈:
#include <iostream>using namespace std;const int N = 100010;int stk[N], tt;int main()
{int n;cin >> n;while (n -- ){int x;scanf("%d", &x);while (tt && stk[tt] >= x) tt -- ;if (!tt) printf("-1 ");else printf("%d ", stk[tt]);stk[ ++ tt] = x;}return 0;
}
这道题的代码: