思路
直接單調棧魔板題
code
#include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for(int i=x;i<=y;i++)
#define frep(i,x,y) for(int i=x;i>=y;i--)
const int N = 100005;
int n;
stack<int> s;
int a[N];
int f[N];
int main() {
scanf("%d", &n);
rep(i, 1, n)
scanf("%d", &a[i]);
frep(i, n, 1) {
while (!s.empty() && a[s.top()] <= a[i]) s.pop();
if (!s.empty()) f[i] = s.top();
else f[i] = 0;
s.push(i);
}
rep(i, 1, n) printf("%d\n", f[i]);
return 0;
}