Files
python/TangDou/DanDiaoZhan/Right_Small.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

36 lines
841 B
C++

#include <bits/stdc++.h>
//https://www.ixigua.com/6925435220292436494
using namespace std;
/**
输入数据:
7
50 30 10 5 3 1 20
输出数据:
30 10 5 3 1 -1 -1
*/
int n;
stack<int> stk; //装的是序号
const int N = 1010;
int a[N]; //a[stk.top()]是指栈顶元素的值
int res[N]; //装的是序号 a[res[i]]是真正的数值
int main() {
cin >> n;
for (int i = 1; i <= n; i++)cin >> a[i];
for (int i = 1; i <= n; i++) {
//a[i]右侧元素,栈顶元素找到了右侧比自己小的最近数据
while (!stk.empty() && a[stk.top()] >= a[i]) {
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;
}