tj
2025-10-26 11:03:19
发布于:北京
4阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
const int N = 3e6 + 5;
struct node{
int id,val;
}a[N],st[N]; //st-栈
int n,top,ans[N]; //top-栈顶标记,ans-答案序列
int main(){
cin >> n;
for(int i = 1;i <= n;i++){
cin >> a[i].val;
a[i].id = i;
}
//单调栈维护 - 单调减
for(int i = 1;i <= n;i++){
while(top > 0 && st[top].val < a[i].val){
//a[i].val 是 st[top].val 在右侧第一个比它大的数字
ans[st[top].id] = a[i].id;
top--; //退栈
}
//要么栈空,要么放进去不影响单调性
st[++top] = a[i];
}
for(int i = 1;i <= n;i++){
cout << ans[i] << " ";
}
return 0;
}
这里空空如也






有帮助,赞一个