Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <algorithm>
  2. #include <string>
  3. #include <set>
  4. #include<iostream>
  5. #include<cstdio>
  6. #include<vector>
  7. #include<queue>
  8.  
  9. using namespace std;
  10.  
  11. int a[1000005], p[1000005];
  12. int main()
  13. {
  14.     int n,m,res = 0,end;
  15.     cin >> n;
  16.     for(int i = 0; i <= n; ++i)
  17.     {
  18.         a[i] = 1e9;
  19.         p[i] = -1;
  20.     }
  21.     a[1] = 0;
  22.     p[1] = 0;
  23.     set<  pair<int, int> > q;
  24.     q.insert(make_pair(0, 1));
  25.     while(!q.empty())
  26.     {
  27.         if(a[n] != 1e9)
  28.             break;
  29.         int x = q.begin()->first, y = q.begin()->second;
  30.         q.erase(q.begin());
  31.         if(a[y + 1] > x + 1)
  32.         {
  33.             if(a[y+1] != 1e9)
  34.                     q.erase(make_pair(a[y+1], y+1));
  35.             a[y+1] = x + 1;
  36.             p[y+1] = y;
  37.             q.insert(make_pair(a[y+1], y+1));
  38.         }
  39.         if(y * 2 <= n && a[y * 2] > x + 1)
  40.         {
  41.             if(a[y* 2] != 1e9)
  42.                     q.erase(make_pair(a[y*2], y*2));
  43.             a[y * 2] = x + 1;
  44.             p[y*2] = y;
  45.             q.insert(make_pair(a[y*2],y*2 ));
  46.         }
  47.        
  48.         if(y * 3 <= n && a[y * 3] > x + 1)
  49.         {
  50.             if(a[y* 3] != 1e9)
  51.                     q.erase(make_pair(a[y*3], y*3));
  52.             a[y * 3] = x + 1;
  53.             p[y * 3] = y;
  54.             q.insert(make_pair( a[y*3],y*3));
  55.         }
  56.     }
  57.     cout << a[n] << endl;
  58.     vector<int> v;
  59.     int x = n;
  60.     while(p[x] != 0)
  61.     {
  62.         v.push_back(x);
  63.         x = p[x];
  64.     }
  65.     reverse(v.begin(), v.end());
  66.     cout << 1 << ' ';
  67.     for(int i = 0; i< v.size(); ++i)
  68.         cout << v[i] << ' ';
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement