Skip to content

Commit fae79ff

Browse files
authored
[20251224] BOJ / G3 / 가장높은탑쌓기 / 한종욱
1 parent 86384c0 commit fae79ff

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 Deque<Integer> deque = new ArrayDeque<>();
9+
private static int[][] towers;
10+
private static int[] dp, history;
11+
private static int N;
12+
13+
public static void main(String[] args) throws IOException {
14+
init();
15+
16+
bw.write(deque.size() + "\n");
17+
while (!deque.isEmpty()) {
18+
bw.write(deque.pollFirst() + "\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+
towers = new int[N][4];
28+
dp = new int[N];
29+
history = new int[N];
30+
31+
Arrays.fill(history, -1);
32+
33+
int len = 0;
34+
int index = -1;
35+
36+
for (int i = 0; i < N; i++) {
37+
StringTokenizer st = new StringTokenizer(br.readLine());
38+
towers[i][0] = Integer.parseInt(st.nextToken());
39+
towers[i][1] = Integer.parseInt(st.nextToken());
40+
towers[i][2] = Integer.parseInt(st.nextToken());
41+
towers[i][3] = i + 1;
42+
}
43+
44+
Arrays.sort(towers, (o1, o2) -> Integer.compare(o2[0], o1[0]));
45+
46+
for (int i = 0; i < N; i++) {
47+
dp[i] = towers[i][1];
48+
49+
for (int j = 0; j < i; j++) {
50+
if (towers[j][2] > towers[i][2]) {
51+
if (dp[j] + towers[i][1] > dp[i]) {
52+
dp[i] = dp[j] + towers[i][1];
53+
history[i] = j;
54+
}
55+
}
56+
}
57+
if (dp[i] > len) {
58+
len = dp[i];
59+
index = i;
60+
}
61+
}
62+
63+
while (index != -1) {
64+
deque.addLast(towers[index][3]);
65+
index = history[index];
66+
}
67+
}
68+
}
69+
```

0 commit comments

Comments
 (0)