题目描述
给定一个长度为N的整数数列,输出每个数左边第一个比它小的数,如果不存在则输出-1。
算法1
C++ 代码
#include <iostream>
using namespace std;
const int N = 100010;
int n;
int a[N];
int stk[N], top;
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ ) cin >> a[i];
for (int i = 0; i < n; i ++ )
{
while (top > 0 && stk[top] >= a[i]) top -- ;
if (top > 0) printf("%d ", stk[top]);
else printf("-1 ");
stk[ ++ top] = a[i];
}
return 0;
}
算法2
C++ 代码
blablabla