求助!
查看原帖
求助!
1271341
chu_yh楼主2024/11/6 17:12

RE #1 #2

#include<bits/stdc++.h>
using namespace std;
const int Max=3e6+5;
int n,a[Max],f[Max];
stack<int> s;

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=n;i++){
		if(s.empty()||a[s.top()]>=a[i]) s.push(i);
		else{
			while(a[s.top()]<a[i]&&!s.empty()){
				f[s.top()]=i;
				s.pop();
			}
			s.push(i);
		}
	}
	for(int i=1;i<n;i++) printf("%d ",f[i]);
	putchar('0');
	return 0;
}
2024/11/6 17:12
加载中...