File tree 4 files changed +43
-0
lines changed
4 files changed +43
-0
lines changed Original file line number Diff line number Diff line change 378
378
| 31 | | [ SWEA-1952 수영장] ( src/DP/swea1952 ) | |
379
379
| 32 | | [ JUNGOL-1681 해밀턴 순환회로] ( ./src/DP/jo1681 ) | TSP |
380
380
| 33 | | [ Baekjoon-2098 외판원 순회] ( ./src/DP/P2098 ) | TSP |
381
+ | 34 | | [ Baekjoon-12852 1로 만들기 2] ( ./src/DP/P12852 ) | |
381
382
382
383
## Time Complexity
383
384
Original file line number Diff line number Diff line change
1
+ package DP .P12852 ;
2
+
3
+ import java .io .*;
4
+
5
+ public class Main {
6
+
7
+ static int N ;
8
+ static int [] cnt = new int [1000001 ], memo = new int [1000001 ];
9
+
10
+ public static void main (String [] args ) throws Exception {
11
+ System .setIn (new FileInputStream ("src/DP/P12852/input.txt" ));
12
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
13
+
14
+ N = Integer .parseInt (br .readLine ());
15
+ for (int i = 2 ; i <= N ; i ++) {
16
+ cnt [i ] = cnt [i -1 ] + 1 ;
17
+ memo [i ] = i -1 ;
18
+ if (i % 3 == 0 && cnt [i ] > cnt [i /3 ] + 1 ) {
19
+ cnt [i ] = cnt [i /3 ] + 1 ;
20
+ memo [i ] = i /3 ;
21
+ }
22
+ if (i % 2 == 0 && cnt [i ] > cnt [i /2 ] + 1 ) {
23
+ cnt [i ] = cnt [i /2 ] + 1 ;
24
+ memo [i ] = i /2 ;
25
+ }
26
+ }
27
+
28
+ StringBuilder sb = new StringBuilder ();
29
+ sb .append (cnt [N ]).append ("\n " );
30
+ while (N >= 1 ) {
31
+ sb .append (N ).append (" " );
32
+ N = memo [N ];
33
+ }
34
+ System .out .println (sb .toString ());
35
+ }
36
+ }
Original file line number Diff line number Diff line change
1
+ ## [ baekjoon-12852] 1로 만들기 2
2
+
3
+ ![ image] ( https://user-images.githubusercontent.com/22045163/112117297-8e487780-8bfe-11eb-8f9b-5516059c6245.png )
4
+
5
+ ![ image] ( https://user-images.githubusercontent.com/22045163/112117342-9ef8ed80-8bfe-11eb-84e7-10e6b908f4c7.png )
Original file line number Diff line number Diff line change
1
+ 10
You can’t perform that action at this time.
0 commit comments