File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed
weekly/week05/BOJ_14002_가장긴증가하는부분수열4 Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ package week05 .BOJ_14002_가장긴증가하는부분수열4 ;
2+
3+ import java .util .*;
4+ import java .lang .*;
5+ import java .io .*;
6+
7+ class BOJ14002 {
8+ public static void main (String [] args ) throws IOException {
9+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
10+ int N = Integer .parseInt (br .readLine ());
11+ StringTokenizer st = new StringTokenizer (br .readLine ());
12+ int [] arr = new int [N ];
13+ for (int i = 0 ; i < N ; i ++) {
14+ arr [i ] = Integer .parseInt (st .nextToken ());
15+ }
16+
17+ int [] dp = new int [N ];
18+ for (int i = 0 ; i < N ; i ++) {
19+ dp [i ] = 1 ;
20+ for (int j = 0 ; j < i ; j ++) {
21+ if (arr [j ] < arr [i ]) {
22+ dp [i ] = Math .max (dp [i ], dp [j ] + 1 );
23+ }
24+ }
25+ }
26+
27+ StringBuilder answer = new StringBuilder ();
28+ int max = 0 ;
29+ for (int i = 0 ; i < N ; i ++) {
30+ if (dp [i ] > max ) {
31+ max = dp [i ];
32+ }
33+ }
34+ answer .append (max ).append ("\n " );
35+
36+ Stack <Integer > stack = new Stack <>();
37+ for (int i = N - 1 ; i >= 0 ; i --) {
38+ if (dp [i ] == max ) {
39+ stack .push (arr [i ]);
40+ max --;
41+ }
42+ }
43+
44+ while (!stack .isEmpty ()) {
45+ answer .append (stack .pop ()).append (" " );
46+ }
47+
48+ System .out .println (answer );
49+ }
50+ }
You can’t perform that action at this time.
0 commit comments