Skip to content

Commit 32fd1e5

Browse files
authored
Merge pull request #727 from AlgorithmWithGod/Ukj0ng
[20250824] BOJ / G4 / Elder price robot / 한종욱
2 parents b91366f + b0e210b commit 32fd1e5

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
```
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
private static int[] arr, answer;
9+
private static Stack<Integer> stack;
10+
private static int N;
11+
12+
public static void main(String[] args) throws IOException {
13+
init();
14+
solve();
15+
16+
for (int i = 0; i < N; i++) {
17+
if (answer[i] != -1) bw.write(answer[i] + "\n");
18+
else bw.write("infinity" + "\n");
19+
}
20+
bw.flush();
21+
bw.close();
22+
br.close();
23+
}
24+
25+
private static void init() throws IOException {
26+
N = Integer.parseInt(br.readLine());
27+
28+
arr = new int[N];
29+
answer = new int[N];
30+
stack = new Stack<>();
31+
Arrays.fill(answer, -1);
32+
33+
StringTokenizer st = new StringTokenizer(br.readLine());
34+
for (int i = 0; i < N; i++) {
35+
arr[i] = Integer.parseInt(st.nextToken());
36+
}
37+
}
38+
39+
private static void solve() {
40+
for (int i = 0; i < N; i++) {
41+
while (!stack.isEmpty() && arr[stack.peek()] >= arr[i]) {
42+
int idx = stack.pop();
43+
answer[idx] = i - idx;
44+
}
45+
stack.push(i);
46+
}
47+
}
48+
}
49+
```

0 commit comments

Comments
 (0)