-
Notifications
You must be signed in to change notification settings - Fork 4
/
Main.java
54 lines (49 loc) · 1.63 KB
/
Main.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
44
45
46
47
48
49
50
51
52
53
54
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
private static int N;
private static ArrayList<int[]> towers;
private static Stack<int[]> res;
private static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
towers = new ArrayList<>();
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
int[] tmp = {Integer.parseInt(st.nextToken()), i};
towers.add(tmp);
}
res = new Stack<>();
sol();
System.out.println(sb);
}
public static void sol() {
for (int[] tower : towers) {
if (res.isEmpty()) {
res.push(tower);
sb.append("0").append(" ");
} else {
while (true) {
if (res.isEmpty()) {
sb.append("0").append(" ");
res.push(tower);
break;
}
int[] before = res.peek();
if (before[0] < tower[0]) {
res.pop();
} else {
res.push(tower);
sb.append(before[1] + 1).append(" ");
break;
}
}
}
}
}
}