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

33 lines
628 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int stk[N], tt;
/**
测试用例:右边第一个比我大
6
6 10 3 7 4 12
结果:
10 12 7 12 12 -1
*/
int a[N], res[N];
int main() {
freopen("830_RightBigger.in", "r", stdin);
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = n; i; i--) {
while (tt && a[i] >= stk[tt]) tt--;
if (tt == 0)
res[i] = -1;
else
res[i] = stk[tt];
stk[++tt] = a[i];
}
for (int i = 1; i <= n; i++) cout << res[i] << " ";
return 0;
}