Skip to content

Commit c565c74

Browse files
authored
[20250718] BOJ / G5 / 옥상 정원 꾸미기 / 이강현
1 parent bacaab7 commit c565c74

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class Main{
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
static int N;
9+
static int[] buildings;
10+
static long ans = 0;
11+
public static void main(String[] args) throws Exception {
12+
N = Integer.parseInt(br.readLine());
13+
buildings = new int[N];
14+
15+
for (int i = 0; i < N; i++) {
16+
buildings[i] = Integer.parseInt(br.readLine());
17+
}
18+
19+
ArrayDeque<Integer> key = new ArrayDeque<>();
20+
ArrayDeque<Integer> value = new ArrayDeque<>();
21+
key.push(0);
22+
value.push(buildings[0]);
23+
for (int i = 1; i < N; i++) {
24+
while(!key.isEmpty() && buildings[i] >= value.peek()){
25+
value.pop();
26+
ans += i - key.pop() - 1;
27+
}
28+
key.push(i);
29+
value.push(buildings[i]);
30+
}
31+
while(!key.isEmpty()){
32+
ans += N - key.pop() - 1;
33+
}
34+
bw.write(ans+"");
35+
bw.close();
36+
}
37+
}
38+
```

0 commit comments

Comments
 (0)