题目描述
给定一个长度为N的整数数列,输出每个数左边第一个比它小的数,如果不存在则输出-1。
输入格式
第一行包含整数N,表示数列长度。
第二行包含N个整数,表示整数数列。
输出格式
共一行,包含N个整数,其中第i个数表示第i个数的左边第一个比它小的数,如果不存在则输出-1。
数据范围
1≤N≤105
1≤数列中元素≤109
所有操作保证合法。
输入样例
5
3 4 2 7 5
输出样例
-1 3 -1 2 2
C++ 代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N=1e5+10;
int x,n,st[N],hh=0;
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>x;
while(hh&&st[hh]>=x) hh--;
if(hh) cout<<st[hh]<<" ";
else cout<<"-1 ";
st[++hh]=x;
}
puts("");
return 0;
}