Skip to the content.

P5788 【模板】单调栈

Link

Solution

单调栈板子题,某谷卡了STL和cincout,所以以下代码T了一个点,把stack用数组模拟应该就能过,但我懒

Code

#include <algorithm>
#include <cstdio>
#include <stack>
using namespace std;

int f[3000010], a[3000010];

int main() {
    int n;
    stack<int> s;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        while (!s.empty() && a[i] > a[s.top()]) f[s.top()] = i, s.pop();
        s.push(i);
    }
    for (int i = 1; i <= n; i++) printf("%d ", f[i]);
    return 0;
}