#10354. 单调栈【模板】
题目网址 https://oj.mimadao.com/problem/10354
上代码:
// A code block
var foo = 'bar';
// An highlighted block
var foo = 'bar';
正解:
#include <bits/stdc++.h>
using namespace std;
const int N = 3e6 + 5;
stack<int> stk;
int a[N], f[N];
int main(void){
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = n; i >= 1; i--) {
while (stk.size() && a[stk.top()] <= a[i]) {
stk.pop();
}
f[i] = (stk.empty() ? 0 : stk.top());
stk.push(i);
}
for (int i = 1; i <= n; i++) {
printf("%d ", f[i]);
}
puts("");
return 0;
}