본문 바로가기
알고리즘/DP

1463 1로 만들기

by tryotto 2019. 2. 8.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <algorithm>
#include <vector>
 
using namespace std;
 
int main() {
    int max = 1000001,n;
    vector<int> dp(max,10000);
 
    dp[1= 0;
    
    scanf("%d"&n);
    getchar();
 
    for (int i = 1; i <= n; i++) {
        if (i * 3 <= max) {
            dp[i * 3= min(dp[i * 3], dp[i] + 1);                                         
        }
        if (i * 2 <= max) {
            dp[i * 2= min(dp[i * 2], dp[i] + 1);            
        }
        if (i +1 <= max) {
            dp[i +1= min(dp[i +1], dp[i] + 1);                
        }
    }
 
    printf("%d", dp[n]);
}
cs


'알고리즘 > DP' 카테고리의 다른 글

9251 LCS  (0) 2019.02.09
1149 RGB거리  (0) 2019.02.09
2600 구슬게임  (0) 2019.02.08
01knapsack 더블릿 (복습 필요)  (0) 2019.02.08
sumset 더블릿  (0) 2019.02.08