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.

31 lines
668 B

#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n;
int a[N];
int stk[N], tt;
int res[N];
/**
测试用例:右边第一个比我大
6
6 10 3 7 4 12
结果:
10 12 7 12 12 -1
*/
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
//答案是查找右侧的,所以正序遍历
for (int i = 1; i <= n; i++) {
while (tt && a[stk[tt]] < a[i]) {//栈内装的是没有找到答案的数字
res[stk[tt]] = i;
tt--;
}
stk[++tt] = i;
}
for (int i = 1; i <= n; i++) res[i] ? cout << a[res[i]] << ' ' : cout << "-1 ";
return 0;
}