题目描述
stack 不再考虑栈的细节
C++ 代码
模拟
#include<iostream>
using namespace std;
const int N=100010;
int s[N],tt;
int n;
int main(){
cin>>n;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
while(tt&&s[tt]>=x)tt--;
if(tt)cout<<s[tt]<<" ";
else cout<<"-1"<<" ";
s[++tt]=x;
}
return 0;
}
C++ 代码
stack
#include<iostream>
#include<stack>
using namespace std;
const int N=100010;
stack<int> s;
//int s[N];
int tt;
int n;
int main()
{
std::ios::sync_with_stdio(false);//取消同步 ,加快读写
cin>>n;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
while(!s.empty() && x<=s.top()) s.pop();//找比x小的元素。所以反过来
if(!s.empty()) cout<<s.top()<<" ";
else cout<<"-1 ";
s.push(x);
}
return 0;
}