AcWing 830. 单调栈
原题链接
简单
作者:
LeonLiu
,
2019-12-04 21:12:06
,
所有人可见
,
阅读 717
import java.io.*;
public class Main{
private static int[] stk = new int[100010];
private static int idx;
public static void main(String[] args) throws IOException{
try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))){
idx = -1;
int n = Integer.parseInt(br.readLine());
String[] l1 = br.readLine().split(" ");
int[] arr = new int[n];
for(int i = 0; i < n; i++) arr[i] = Integer.parseInt(l1[i]);
StringBuilder ans = new StringBuilder(4 * 100000);
for(int i = 0; i < n; i++){
while(idx != -1 && stk[idx] >= arr[i]){
idx--;
}
ans.append(idx == -1 ? -1 : stk[idx]).append(" ");
stk[++idx] = arr[i];
}
System.out.println(ans);
}
}
}