Skip to content

Commit 2a047b8

Browse files
committed
[20251025] BOJ / G4 / 가장 긴 증가하는 부분 수열 4 / 김민진
1 parent 0d600f2 commit 2a047b8

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
```java
2+
import java.io.*;
3+
import java.util.Stack;
4+
import java.util.StringTokenizer;
5+
6+
public class BJ_14002_가장_긴_증가하는_부분_수열_4 {
7+
8+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
10+
private static final StringBuilder sb = new StringBuilder();
11+
private static StringTokenizer st;
12+
13+
private static int N, lis;
14+
private static int[] nums, dp;
15+
private static Stack<Integer> stack;
16+
17+
public static void main(String[] args) throws IOException {
18+
init();
19+
sol();
20+
}
21+
22+
private static void init() throws IOException {
23+
N = Integer.parseInt(br.readLine());
24+
25+
nums = new int[N];
26+
st = new StringTokenizer(br.readLine());
27+
for (int i = 0; i < N; i++) {
28+
nums[i] = Integer.parseInt(st.nextToken());
29+
}
30+
31+
dp = new int[N];
32+
dp[0] = 1;
33+
stack = new Stack<>();
34+
}
35+
36+
private static void sol() throws IOException {
37+
getLis();
38+
getArr();
39+
40+
sb.append(lis).append("\n");
41+
while (!stack.isEmpty()) {
42+
sb.append(stack.pop()).append(" ");
43+
}
44+
45+
bw.write(sb.toString());
46+
bw.flush();
47+
bw.close();
48+
br.close();
49+
}
50+
51+
private static void getLis() {
52+
lis = 1;
53+
for (int i = 1; i < N; i++) {
54+
dp[i] = 1;
55+
for (int j = 0; j < i; j++) {
56+
if (nums[i] > nums[j]) {
57+
dp[i] = Math.max(dp[i], dp[j] + 1);
58+
lis = Math.max(lis, dp[i]);
59+
}
60+
}
61+
}
62+
}
63+
64+
private static void getArr() {
65+
int targetLen = lis;
66+
67+
for (int i = N - 1; i >= 0; i--) {
68+
if (dp[i] == targetLen) {
69+
stack.push(nums[i]);
70+
targetLen--;
71+
}
72+
}
73+
}
74+
}
75+
```

0 commit comments

Comments
 (0)