博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #295 (Div. 2)B - Two Buttons BFS
阅读量:6568 次
发布时间:2019-06-24

本文共 1899 字,大约阅读时间需要 6 分钟。

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.

Sample test(s)
Input
4 6
Output
2
Input
10 1
Output
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.

两种操作:

1.乘以2

2.减一

然后直接跑一发bfs就好啦

struct node{    int x;    int step;};int vis[maxn];int n,m;int main(){    cin>>n>>m;    queue
q; q.push({n,0}); vis[n]=1; while(!q.empty()) { node now=q.front(); q.pop(); if(now.x==m) { cout<
<
maxn-1||next.x<0) continue; vis[next.x]=1; q.push(next); } else { next.x--; if(vis[next.x]||next.x>maxn-1||next.x<0) continue; vis[next.x]=1; q.push(next); } } }}

 

转载地址:http://gypjo.baihongyu.com/

你可能感兴趣的文章
Chromium Graphics: 3D上下文及其虚拟化 - Part I
查看>>
jquery javascript获得网页的高度和宽度
查看>>
2019 -2-15 复习
查看>>
vim锁定屏幕
查看>>
实用的 JavaScript 调试小技巧
查看>>
027移除元素
查看>>
Linux下清理内存和Cache方法
查看>>
CodeVS 1018 单词接龙(DFS)
查看>>
我的博客园的CSS和html设置
查看>>
工作中简单的kettle使用
查看>>
spark shuffle:分区原理及相关的疑问
查看>>
Laravel5.5 使用第三方Vendor添加注册验证码
查看>>
06- Linux下sublime下载与使用
查看>>
前端文摘:Web 开发模式演变历史和趋势
查看>>
将图片序列转化为视频文件
查看>>
jQuery的文档操作***
查看>>
js 小数取整,js 小数向上取整,js小数向下取整
查看>>
vue-cli3.0
查看>>
window.location.replace vs window.location.href
查看>>
CVPR 2018:阿里提出应用 LocalizedGAN 进行半监督训练
查看>>