B. Two Buttons

B. Two Buttons
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n.

Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result?

Input

The first and the only line of the input contains two distinct integers n and m (1 ≤ n, m ≤ 104), separated by a space .

Output

Print a single number — the minimum number of times one needs to push the button required to get the number m out of number n.

Examples
input
Copy
4 6
output
Copy
2
input
Copy
10 1
output
Copy
9
Note

In the first example you need to push the blue button once, and then push the red button once.

In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.

先说题意,求n转换到m的步骤数,n只能自己*2,和减一。

n>m不用说了,答案n-m。

道题能用两种方法,第一种是Greedy,第二种是BFS。

第一种

我们来反着想,由m到n,也就是m只能+1或者/2.

如果m是奇数,那么必然最后一步是减操作,我们可以确定,因此可以先加回去m变成m+1

如果是偶数,那么必然最后一步是乘以2也可以还原,把m变成m/2


  1. #include<cstdio>  
  2. #include<cstring>  
  3. #include<algorithm>  
  4. #include<iostream>  
  5. #include<string>  
  6. #include<vector>  
  7. #include<stack>  
  8. #include<bitset>  
  9. #include<cstdlib>  
  10. #include<cmath>  
  11. #include<set>  
  12. #include<list>  
  13. #include<deque>  
  14. #include<map>  
  15. #include<bits/stdc++.h>  
  16. using namespace std;  
  17. int main() {  
  18.     string s;  
  19.     int man = 0, ans = 0, n, m;  
  20.     int a[1000];//red button : *2  
  21.     //blue button:-1  
  22.     while(cin >> n >> m) {  
  23.         ans = 0;  
  24.         if(n > m) {  
  25.             printf("%d\n", n - m);  
  26.         } else {  
  27.             while(n < m) {  
  28.                 if(m % 2 != 0) {  
  29.                     m++;  
  30.                     ans++;  
  31.                 }  
  32.                 m /= 2;  
  33.                 ans++;  
  34.             }  
  35.             ans+=n-m;  
  36.             printf("%d\n", ans);  
  37.         }  
  38.     }  
  39. }  


第二种,构造成二叉树,用BFS去写,其实问题都可以模拟其过程去抽象,抽象成一个树,不是 dfs bfs只能用于图论。

还有就是注意标记不要重复搜索。

这里在记录一下pair的使用,make_pair是直接创建一个对象,无需声明变量类型。

  1. #include<cstdio>  
  2. #include<cstring>  
  3. #include<algorithm>  
  4. #include<iostream>  
  5. #include<string>  
  6. #include<vector>  
  7. #include<stack>  
  8. #include<bitset>  
  9. #include<cstdlib>  
  10. #include<cmath>  
  11. #include<set>  
  12. #include<list>  
  13. #include<deque>  
  14. #include<map>  
  15. #include<queue>  
  16. using namespace std;  
  17. #define mp(a,b) make_pair(a, b)  
  18. typedef pair<intint> pii ;  
  19. typedef long long ll;  
  20. int n, m;  
  21. int vis[100000];  
  22. int bfs() {  
  23.     queue<pii>q;  
  24.     q.push(mp(0, n));  
  25.     memset(vis,0,sizeof(vis));  
  26.     while(!q.empty()) {  
  27.         pii tmp = q.front();  
  28.         q.pop();  
  29.         //first_road  
  30.         pii tmp1 = mp(tmp.first + 1, tmp.second * 2);  
  31.         if(tmp1.second >= 1 && tmp1.second <= 10000 && !vis[tmp1.second]) {  
  32.             if(tmp1.second == m)return tmp1.first;  //
  33.             vis[tmp1.second] = 1;  
  34.             q.push(tmp1);  
  35.         }  
  36.         //second_road  
  37.         tmp1 = mp(tmp.first + 1, tmp.second - 1);  
  38.         if(tmp1.second >= 1 && tmp1.second <= 10000 && !vis[tmp1.second]) {  //注意范围
  39.             if(tmp1.second == m)return tmp1.first;  //
  40.             vis[tmp1.second] = 1;  
  41.             q.push(tmp1);  
  42.         }  
  43.     }  
  44. }  
  45. int main() {  
  46.     while(cin >> n >> m) {  
  47.         if(n >= m) {  
  48.             cout << n - m << endl;  
  49.         } else {  
  50.             cout << bfs();  
  51.         }  
  52.     }  
  53. }  


B. Two Buttons