Files
python/TangDou/AcWing/Stack/830_1.cpp

31 lines
668 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#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;
}