bdfz_2024_summer/day14/P5788/P5788.cpp
2024-08-18 12:06:33 +08:00

50 lines
880 B
C++

#include <cctype>
#include <cstdio>
#include <iostream>
#include <stack>
using namespace std;
const int MAX_N=3e6+5;
int readInt();
int a[MAX_N];
int ans[MAX_N];
stack<int>s;
int main(){
const int n=readInt();
for(int i=1;i<=n;i++){
a[i]=readInt();
}
ans[n]=0;
s.push(n);
for(int i=n-1;i>=1;i--){
if (a[i]>=a[s.top()]) {
ans[i]=0;
s.push(i);
}else{
ans[i]=s.top();
while (s.size()>0&&a[s.top()]>a[i]) {
s.pop();
}
s.push(i);
}
}
for(int i=1;i<=n;i++){
cout<<ans[i]<<" ";;
}
cout<<endl;
}
int readInt(){
int x=0,w=1;
char ch=0;
while (!isdigit(ch)) {
if(ch=='-')w=-1;
ch=getchar();
}
while(isdigit(ch)){
x=x*10+(ch-'0');
ch=getchar();
}
return x*w;
}