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.
36 lines
844 B
36 lines
844 B
#include <bits/stdc++.h>
|
|
//https://www.ixigua.com/6925435220292436494
|
|
|
|
using namespace std;
|
|
/**
|
|
输入数据:
|
|
7
|
|
50 30 10 5 3 1 20
|
|
|
|
输出数据:
|
|
-1 -1 20 20 20 20 -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;
|
|
} |