Skip to content

Commit f47e2f0

Browse files
committed
[BOJ]가장 긴 증가하는 부분 수열4/골드4/2시간
1 parent fa7f6bd commit f47e2f0

File tree

1 file changed

+53
-0
lines changed
  • junsu/week13/BOJ14002 가장 긴 증가하는 부분 수열4

1 file changed

+53
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//BOJ14002 가장 긴 증가하는 부분 수열4, 골드4
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class BOJ14002 {
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
9+
StringTokenizer st;
10+
11+
int N = Integer.parseInt(br.readLine());
12+
int[] A = new int[N+1];
13+
int[] dp = new int[N+1];
14+
15+
st = new StringTokenizer(br.readLine());
16+
for(int i = 1; i <= N; i++) {
17+
A[i] = Integer.parseInt(st.nextToken());
18+
dp[i] = 1;
19+
}
20+
// System.out.println(Arrays.toString(A));
21+
22+
int max = 1;
23+
for(int i = 1; i <= N; i++) {
24+
for(int j = 1; j < i; j++) {
25+
if(A[i] > A[j]) {
26+
dp[i] = Math.max(dp[i], dp[j]+1); // 이전 원소들 중 가장 큰 dp값 + 1
27+
}
28+
}
29+
max = Math.max(max, dp[i]);
30+
}
31+
32+
bw.write(max+"\n");
33+
int value = max;
34+
Stack<Integer> stack = new Stack<>();
35+
36+
for(int i = N; i >= 1; i--) {
37+
if(value == dp[i]) {
38+
stack.push(A[i]);
39+
value--;
40+
}
41+
}
42+
43+
while(!stack.isEmpty()) {
44+
bw.write(stack.pop() +" ");
45+
}
46+
47+
48+
bw.write("\n");
49+
bw.flush();
50+
bw.close();
51+
br.close();
52+
}
53+
}

0 commit comments

Comments
 (0)