#7~10WA了,求大佬看看@-@

P5788 【模板】单调栈

说实话,没看懂
by zhangbomingpp @ 2024-02-20 20:50:36


我把我的代码加上注释给你看看,重要的是理解思路
by zhangbomingpp @ 2024-02-20 20:51:19


```cpp #include <bits/stdc++.h> using namespace std; int n , a[3000007] , ans[3000007]; stack <int> s; int main() { cin >> n; for (int i = 1 ; i <= n ; i++) cin >> a[i]; for (int i = n ; i >= 1 ; i--) { // 这题我写的逆序遍历(老师要这么写的,别怪我) while (!s.empty() && a[s.top()] <= a[i]) s.pop(); // 小的都pop掉,剩下的就是大的 if (!s.empty()) ans[i] = s.top(); s.push(i); // 因为有可能有重复的数,所以单调栈存下标 } for (int i = 1 ; i <= n ; i++) cout << ans[i]; return 0; } ```
by zhangbomingpp @ 2024-02-20 20:57:35


@[zhangbomingpp](/user/945845) 谢谢提供思路!ac啦~
by saurrrry @ 2024-02-22 20:15:23


|