- 编程
单调栈模板代码
- 2023-3-25 15:55:23 @
#include<cstdio>
using namespace std;
const int N = 3000010;
int stack[N] , top = -1;
int a[N];
int ans[N];
int main(){
int n ;
cin >> n;
for(int i = 1; i <= n ; i ++){
scanf("%d" , &a[i]);
}
for(int i = n; i >= 1; i--){
while(top >= 0 && a[i] >= a[stack[top]])
{
top --;//出栈操作
}
top ++;
stack[top] = i;//stack里存的是下标
if(top <= 0){
ans[i] = 0;
}else {
ans[i] = stack[top-1];
}
}
for(int i = 1 ; i <= n ; i ++){
printf("%d " , ans[i]);
}
return 0;
}
0 条评论
目前还没有评论...