题意:
现在给你n个数字: ,问从每个数字往后看,第一个比他大的数字的下标是多少。
题解:
使用一个单调递减栈即可。
#include<bits/stdc++.h>
using namespace std;
const int N=100005;
int n,s[N],a[N],ans[N],top=0;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",a+i);
}
for(int i=1;i<n;i++){
while(top&&a[s[top]]<a[i]){
ans[s[top]]=i;
--top;
}
s[++top]=i;
}
for(int i=1;i<=top;i++){
ans[s[i]]=0;
}
for(int i=1;i<=n;i++){
printf("%d ",ans[i]);
}
return 0;
}