Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <string>
- #include <set>
- #include<iostream>
- #include<cstdio>
- #include<vector>
- #include<queue>
- using namespace std;
- int a[1000005], p[1000005];
- int main()
- {
- int n,m,res = 0,end;
- cin >> n;
- for(int i = 0; i <= n; ++i)
- {
- a[i] = 1e9;
- p[i] = -1;
- }
- a[1] = 0;
- p[1] = 0;
- set< pair<int, int> > q;
- q.insert(make_pair(0, 1));
- while(!q.empty())
- {
- if(a[n] != 1e9)
- break;
- int x = q.begin()->first, y = q.begin()->second;
- q.erase(q.begin());
- if(a[y + 1] > x + 1)
- {
- if(a[y+1] != 1e9)
- q.erase(make_pair(a[y+1], y+1));
- a[y+1] = x + 1;
- p[y+1] = y;
- q.insert(make_pair(a[y+1], y+1));
- }
- if(y * 2 <= n && a[y * 2] > x + 1)
- {
- if(a[y* 2] != 1e9)
- q.erase(make_pair(a[y*2], y*2));
- a[y * 2] = x + 1;
- p[y*2] = y;
- q.insert(make_pair(a[y*2],y*2 ));
- }
- if(y * 3 <= n && a[y * 3] > x + 1)
- {
- if(a[y* 3] != 1e9)
- q.erase(make_pair(a[y*3], y*3));
- a[y * 3] = x + 1;
- p[y * 3] = y;
- q.insert(make_pair( a[y*3],y*3));
- }
- }
- cout << a[n] << endl;
- vector<int> v;
- int x = n;
- while(p[x] != 0)
- {
- v.push_back(x);
- x = p[x];
- }
- reverse(v.begin(), v.end());
- cout << 1 << ' ';
- for(int i = 0; i< v.size(); ++i)
- cout << v[i] << ' ';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement