#include <iostream>
#include <stack>
using namespace std;
int main()
{
int n;
cin>>n;
int a[101];
int j=1;
stack <int> s;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
while(j<=n)
{
int Max=a[j];
for(int k=j+1;k<=n;k++)
{
Max=max(a[k],Max);
}
if (!s.empty() && s.top()>Max)
{
printf("%d ",s.top());
s.pop();
continue;
}
for(;a[j]!=Max;j++)
{
s.push(a[j]);
}
printf("%d ",Max);
j++;
}
while(!s.empty())
{
printf("%d ",s.top());
s.pop();
}
return 0;
}
18715 出栈序列
最新推荐文章于 2024-10-09 09:53:23 发布