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个数 当做按顺序 ...
随机推荐
- SpringMVC类型转换、数据绑定详解[附带源码分析]
目录 前言 属性编辑器介绍 重要接口和类介绍 部分类和接口测试 源码分析 编写自定义的属性编辑器 总结 参考资料 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那 ...
- 我的权限系统设计实现MVC4 + WebAPI + EasyUI + Knockout(四)授权代码维护
一.前言 权限系统设计中,授权代码是用来控制数据访问权限的.授权代码说白了只是一树型结构的数据,没有什么其它的业务意义.那么这个页面的功能也就非常简单授权代码维护:新增.修改.删除授权代码数据. 二. ...
- JavaScript并非“按值传递”
置顶文章:<纯CSS打造银色MacBook Air(完整版)> 上一篇:<拥Bootstrap入怀--模态框(modal)篇> 作者主页:myvin 博主QQ:85139910 ...
- DOM(三)使用DOM + Css
1.使用getElementsByTagName修改class类别或者追加类别 <ul class="name1" onclick="clickz()"& ...
- AJAX(一)AJAX的简介和基础
本节简介(异步链接服务器对象)XMLHTTPRequest以及AJAX的简介. AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML). AJ ...
- C#的默认访问修饰符
Classes and structs that are not nested within other classes or structs can be either public or inte ...
- hdu1853 km算法
//hdu1853 #include<stdio.h> #include<string.h> #define INF 99999999 ][],pr[],pl[],visr[] ...
- Google-解决在调试页面或者js时总是提示烦恼的断点问题
按F12键,然后切换到Source标签,看底下的那个跟暂停一样的图标是不是变成蓝色或紫色了? 如果是蓝色或者紫色,则把他切换到“灰色”状态(点击图标就会切换成不同的状态.或者可能是其他颜色状态),如下 ...
- 【kAri OJ605】陈队的树
时间限制 1000 ms 内存限制 65536 KB 题目描述 陈队有N棵树,有一天他突然想修剪一下这N棵树,他有M个修剪器,对于每个修剪器给出一个高度h,表示这个修剪器可以把某一棵高度超过h的树修剪 ...
- Teradata 语句简单优化
Teradata 基本查询语言和SQL server 是一致的.有很小的区别. 功能没有SQL 全面,界面没有SQL 好看~ 1. teradata 里面经常会报一种错误: no enough spo ...