cf.295.B Two Buttons (bfs)
2 seconds
256 megabytes
standard input
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?
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.
#include<stdio.h>
#include<queue>
#include<set>
using namespace std;
int n , m ; int main ()
{
//freopen ("a.txt" , "r" , stdin) ;
while (~ scanf ("%d%d" , &n , &m)) {
queue <pair <int , int> > q ;
set <int> s ;
q.push ( {n , } ) ;
while (!q.empty ()) {
pair <int , int> p = q.front () ;
q.pop () ;
if (p.first <= )
continue ;
if (p.first == m) {
printf ("%d\n" , p.second) ;
break ;
}
else {
s.insert (p.first) ;
if (s.count (p.first - ) == ) {
q.push ( {p.first - , p.second + } ) ;
}
if (p.first < m && s.count (p.first * ) == ) {
q.push ( {p.first * , p.second + } ) ;
}
}
}
}
return ;
}
我是用bfs(看别人的),见识了set 和 pair 的用法。
There is, however, an even faster solution. The problem can be reversed as follows: we should get the number n starting from m using the operations "add 1 to the number" and "divide the number by 2 if it is even".
cf.295.B Two Buttons (bfs)的更多相关文章
- CF 520 B. Two Buttons(bfs)
/*题意:一个数,就是输入的第一个数,让它变成第二个数最少用几步.可以点红色按钮,蓝色按钮来改变数字,红色:*2,蓝色:-1,如果变成负数,就变成原来的数.CF 520 B. Two Buttons思 ...
- CF Two Buttons (BFS)
Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- Codeforces Round #295 (Div. 2)B - Two Buttons BFS
B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- cf 295 div 2 B (bfs)
题意:给出 n.m 两数,可以对 n 进行两种操作 减一或者乘二,操作过程中 n 必须保证非负,问使 n 变为 m 至少需要几步操作. 这是我练水题的时候做到的,题目不难,只是我 bfs 一直没怎么用 ...
- CF #375 (Div. 2) D. bfs
1.CF #375 (Div. 2) D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...
- CF 986A Fair——多源bfs
题目:http://codeforces.com/contest/986/problem/A 如果从每个村庄开始bfs找货物,会超时. 发现k较小.那就从货物开始bfs,给村庄赋上dis[ 该货物 ] ...
- CF G. Orientation of Edges BFS
来两遍 $BFS,$ 都贪心一下即可. #include <bits/stdc++.h> #define maxn 300009 using namespace std; void set ...
- cf.295.C.DNA Alignment(数学推导)
DNA Alignment time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- CF#541 D. Gourmet choice /// BFS 拓扑
题目大意: 给定n m 第一行有n个数 第二行有m个数 接下来n行每行m列 有 = < > 位于 i j 的符号表示 第一行第i个数与第二行第j个数的大小关系 1.将n+m个数 当做按顺序 ...
随机推荐
- 4.HBase In Action 第一章-HBase简介(1.1.2 数据创新)
As we now know, many prominent internet companies, most notably Google, Amazon, Yahoo!, and Facebook ...
- 报错:'Could not load NIB in bundle: 'NSBundle解决办法
1.首先检查拼写是否正确: 2.断开连线,重新连接view与files' owner; 3.规避敏感View名.Xcode中有许多名字是系统预留的.你如果用了也会报这个错误.
- C#基础之lock
1.lock的本质 实现线程同步的第一种方式是我们经常使用的lock关键字,它将包围的语句块标记为临界区,这样一次只有一个线程进入临界区并执行代码.下面第一段的几行代码是关于lock关键字的使用方式, ...
- [转]Extundelete--数据恢复软件
前言 作为一名运维人员,保证数据的安全是根本职责,所以在维护系统的时候,要慎之又慎,但是有时难免会出现数据被误删除的情况,在这个时候该如何快速.有效地恢复数据显得至关重要,extundelete就是其 ...
- [网站公告]23:00-05:00阿里云SLB升级会造成4-8次每次10秒的闪断
大家好,阿里云将于今天夜里(7月29日23:00-7月30日05:00)对负载均衡服务(SLB)进行升级操作,升级期间我们使用的SLB实例会有4-8次的闪断,每次闪断时间10秒左右.闪断期间会造成网站 ...
- C# Winform实现炫酷的透明动画界面
做过.NET Winform窗体美化的人应该都很熟悉UpdateLayeredWindow吧,UpdateLayeredWindow可以实现窗体的任意透明,效果很好,不会有毛边.不过使用这个API之后 ...
- [转]Android Studio 快捷键整理分享
Alt+回车 导入包,自动修正 Ctrl+N 查找类 Ctrl+Shift+N 查找文件 Ctrl+Alt+L 格式化代码 Ctrl+Alt+O 优化导入的类和包 Alt+Insert 生成代码 ...
- Java基础-常量池
在class文件中,“常量池”是最复杂也最值得关注的内容. Java是一种动态连接的语言,常量池的作用非常重要,常量池中除了包含代码中所定义的各种基本类型(如int.long等等)和对象型(如Stri ...
- MAC OS下安装Erlang
这是个很大的问题,也是个很小的问题,恩.因为我最终解决方案是用了别人写的工具,名叫erlbrew: https://github.com/mrallen1/erlbrew 按照页面上的指导安装使用即可 ...
- Laravel5.1 启动详解
借鉴: Laravel所有请求的入口文件是:/public/index.php,代码如下 <?php /*|------------------------------------------- ...