#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
int main()
{
int n;
cin >> n;
vector <int> v(n+1),res(n+1);
for (int i = 1; i <= n; i++)
scanf("%d",&v[i]);
stack<int> st;
for (int i = 1; i <= n; i++)
{
//int top = st.top(); // 这样操作就不行,离谱
// 注意这里存储的是序号,不是数值。如果序列为空 或者 栈顶元素大于当前元素
if (st.empty() || v[st.top()] >= v[i])
st.push(i);
else
{
while(!st.empty() && v[st.top()] < v[i])
{
res[st.top()] = i;
st.pop();
}
st.push(i);
}
}
for (int i = 1; i <= n-1; i++)
printf("%d ", res[i]);
cout << 0;
return 0;
}
05-16
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交