-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13549.cpp
54 lines (54 loc) · 1.24 KB
/
13549.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <cstdio>
#include <queue>
using namespace std;
int d[100002];
int n,k;
queue<int> q;
int main() {
scanf("%d%d",&n,&k);
for(int i=0;i<=100000;++i) d[i]=-1;
d[n] = 0;
q.push(n);
if(n != 0) {
n<<=1;
while(n<=100000) {
if(d[n] == -1){
d[n] = d[n>>1];
q.push(n);
}
n<<=1;
}
}
while(!q.empty()) {
int p = q.front(); q.pop();
int prev = p-1, next = p+1;
if(p>0 && d[prev] == -1) {
d[prev] = d[p]+1;
q.push(prev);
if(prev != 0){
prev<<=1;
while(prev<=100000) {
if(d[prev] == -1){
d[prev] = d[prev>>1];
q.push(prev);
}
prev<<=1;
}
}
}
if(p<100000 && d[next] == -1){
d[next] = d[p]+1;
q.push(next);
next<<=1;
while(next<=100000) {
if(d[next] == -1){
d[next] = d[next>>1];
q.push(next);
}
next<<=1;
}
}
}
printf("%d", d[k]);
return 0;
}