-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaekjoon_2493_2.java
More file actions
65 lines (46 loc) · 1.59 KB
/
baekjoon_2493_2.java
File metadata and controls
65 lines (46 loc) · 1.59 KB
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
55
56
57
58
59
60
61
62
63
64
65
import java.util.*;
import java.io.*;
public class baekjoon_2493_2 {
public static void main(String[] args) throws Exception {
Stack<Tower> senderStack = new Stack<>();
Stack<Tower> receiverStack = new Stack<>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] answer = new int[N];
for(int i=0;i<N;i++) {
int height = Integer.parseInt(st.nextToken());
receiverStack.push(new Tower(height, i + 1));
}
while(!receiverStack.empty()) {
Tower receiver = receiverStack.pop();
if(senderStack.empty()) {
senderStack.push(receiver);
continue;
}
while(receiver.height > senderStack.peek().height) {
Tower sender = senderStack.pop();
answer[sender.idx - 1] = receiver.idx;
if(senderStack.empty()) {
break;
}
}
senderStack.push(receiver);
}
while(!senderStack.empty()) {
Tower sender = senderStack.pop();
answer[sender.idx - 1] = 0;
}
for(int a : answer) {
System.out.print(a + " ");
}
}
public static class Tower {
public int idx;
public int height;
public Tower(int height, int idx) {
this.idx = idx;
this.height = height;
}
}
}