You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
651 B

#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int a[N], res[N];
stack<int> stk;
int n;
/**
输入数据:
7
50 30 10 5 3 1 20
输出数据:
-1 50 30 10 5 3 30
*/
int main() {
cin >> n;
for (int i = 1; i <= n; i++)cin >> a[i];
for (int i = n; i >= 1; i--) {
//a[i]左侧元素,栈顶元素找到了左侧比自己大的最近元素
while (!stk.empty() && a[i] >= a[stk.top()]) {
res[stk.top()] = i;
stk.pop();
}
stk.push(i);
}
for (int i = 1; i <= n; i++)
if (res[i])cout << a[res[i]] << " ";
else cout << "-1 ";
return 0;
}