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?
The first and the only line of the input contains two distinct integers n and m (1 ≤ n, m ≤ 104), separated by a space .
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.
4 6
2
10 1
9
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
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<iostream>
- #include<string>
- #include<vector>
- #include<stack>
- #include<bitset>
- #include<cstdlib>
- #include<cmath>
- #include<set>
- #include<list>
- #include<deque>
- #include<map>
- #include<bits/stdc++.h>
- using namespace std;
- int main() {
- string s;
- int man = 0, ans = 0, n, m;
- int a[1000];//red button : *2
- //blue button:-1
- while(cin >> n >> m) {
- ans = 0;
- if(n > m) {
- printf("%d\n", n - m);
- } else {
- while(n < m) {
- if(m % 2 != 0) {
- m++;
- ans++;
- }
- m /= 2;
- ans++;
- }
- ans+=n-m;
- printf("%d\n", ans);
- }
- }
- }
还有就是注意标记不要重复搜索。
这里在记录一下pair的使用,make_pair是直接创建一个对象,无需声明变量类型。
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<iostream>
- #include<string>
- #include<vector>
- #include<stack>
- #include<bitset>
- #include<cstdlib>
- #include<cmath>
- #include<set>
- #include<list>
- #include<deque>
- #include<map>
- #include<queue>
- using namespace std;
- #define mp(a,b) make_pair(a, b)
- typedef pair<int, int> pii ;
- typedef long long ll;
- int n, m;
- int vis[100000];
- int bfs() {
- queue<pii>q;
- q.push(mp(0, n));
- memset(vis,0,sizeof(vis));
- while(!q.empty()) {
- pii tmp = q.front();
- q.pop();
- //first_road
- pii tmp1 = mp(tmp.first + 1, tmp.second * 2);
- if(tmp1.second >= 1 && tmp1.second <= 10000 && !vis[tmp1.second]) {
- if(tmp1.second == m)return tmp1.first; //
- vis[tmp1.second] = 1;
- q.push(tmp1);
- }
- //second_road
- tmp1 = mp(tmp.first + 1, tmp.second - 1);
- if(tmp1.second >= 1 && tmp1.second <= 10000 && !vis[tmp1.second]) { //注意范围
- if(tmp1.second == m)return tmp1.first; //
- vis[tmp1.second] = 1;
- q.push(tmp1);
- }
- }
- }
- int main() {
- while(cin >> n >> m) {
- if(n >= m) {
- cout << n - m << endl;
- } else {
- cout << bfs();
- }
- }
- }