Skip to content

Commit

Permalink
12852 1로 만들기 2 / 다이나믹 프로그래밍, 그래프
Browse files Browse the repository at this point in the history
  • Loading branch information
cjy8922 authored May 2, 2021
1 parent 0526c97 commit 2cf7064
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions 2021_05/BJ_12852.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int n;
cin >> n;

vector<int> res(n + 1);
vector<int> track(n + 1);

for (int i = 2; i <= n; i++) {
res[i] = res[i - 1] + 1;
track[i] = i - 1;

if (i % 2 == 0 && res[i] > res[i / 2] + 1) {
res[i] = res[i / 2] + 1;
track[i] = i / 2;

}

if (i % 3 == 0 && res[i] > res[i / 3] + 1) {
res[i] = res[i / 3] + 1;
track[i] = i / 3;
}
}

cout << res[n] << endl;

cout << n << " ";
while (n != 1) {
cout << track[n] << " ";
n = track[n];
}


return 0;
}

0 comments on commit 2cf7064

Please sign in to comment.