【AC】
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int n; 5 const int maxn=1e5+2; 6 struct node 7 { 8 int x; 9 int id; 10 node(){} 11 node(int _x,int _id):x(_x),id(_id){} 12 }a[maxn]; 13 int pos[maxn]; 14 int ans[maxn]; 15 int main() 16 { 17 while(~scanf("%d",&n)) 18 { 19 memset(pos,0,sizeof(pos)); 20 memset(ans,0,sizeof(ans)); 21 for(int i=1;i<=n;i++) 22 { 23 scanf("%d",&a[i].x); 24 a[i].id=i; 25 } 26 int top=1; 27 pos[top]=1; 28 for(int i=2;i<=n;i++) 29 { 30 while(top>=1&&(a[i].x<a[pos[top]].x||a[i].x==0)) 31 { 32 ans[pos[top--]]=a[i].id; 33 } 34 pos[++top]=i; 35 } 36 while(top>=1) 37 { 38 ans[pos[top--]]=n+1; 39 } 40 for(int i=1;i<=n;i++) 41 { 42 cout<<ans[i]<<" "; 43 } 44 cout<<endl; 45 } 46 return 0; 47 }
单调栈
时间: 2024-10-06 12:09:20