-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path241130-2493.java
43 lines (39 loc) · 1.26 KB
/
241130-2493.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
Stack<Node> stack = new Stack<>();
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= N; i++) {
int h = Integer.parseInt(st.nextToken());
while (true) {
if (stack.isEmpty()) {
sb.append("0 ");
stack.push(new Node(i, h));
break;
}
if (stack.peek().height >= h) {
sb.append(stack.peek().num).append(" ");
stack.push(new Node(i, h));
break;
}
stack.pop();
}
}
System.out.print(sb);
}
static class Node {
int num;
int height;
public Node(int num, int height) {
this.num = num;
this.height = height;
}
}
}