#include <bits/stdc++.h>
#define int long long
using namespace std ;
int n , top ;
const int pp = 3e6+10 ;
int a[pp] , b[pp],sta[pp] , stai[pp];
int x ;
signed main(){
top = 0 ;
int n ;
scanf("%lld" , &n );
//输入
for( int i = 1 ; i <= n ; i ++ ){
scanf("%lld" , &a[i] );
}
for( int i = n ; i >= 1 ; i -- ){
//特判:若栈为空
if( top == 0 ){
b[i] = 0 ;
sta[top] = a[i] ;
stai[top] = i ;
top++;
}//stai为记录栈中元素在原数组中下标的数组.个人习惯将插入的元素插入在栈顶,然后移动栈顶
else{
//由于只有大于才算,所以这里的判断我用了>=
while(a[i] >= sta[top]){
top -- ;
//不太会写所以直接移动栈顶忽略栈顶原本元素,后面输入再覆盖
}
//因为前面判断为>=,所以跳出while循环中之后栈顶a[i]就会严格小于栈顶
top ++ ;
sta[top] = a[i];
stai[top] = i ;
b[i] = stai[top-1];
}
}
//输出
for( int i = 1 ; i <= n ; i ++ ){
printf("%lld " ,b[i]);
}
printf("\n");
return 0 ;
}
第一个测试点过了,这是我的代码的第二个测试点:
输入:10 703618305(亿) 710496869(亿) 575261280(亿) 476513281(亿) 22822501(千万) 81218209(千万) 60406751(千万) 34397470(千万) 384845191(亿) 9193626(百万)
输出:2 0 0 0 6 9 9 9 0 0
感谢大佬的帮助!好人一生平安!