/*题意:一个数,就是输入的第一个数,让它变成第二个数最少用几步。可以点红色按钮,蓝色按钮来改变数字,红色:*2,蓝色:-1,如果变成负数,
就变成原来的数。
CF 520 B. Two Buttons
思路:当然是*2可以掠过的步数更少啦,如果n是输入,m是输出。

如果n大于m,不能使用红色按钮,很容易看出,步数就是n-m。

如果n比m小,如果m是偶数的话,m/2,如果m是奇数,m+1,这样一直循环判断n是不是还比m小,不符合就跳出循环,进入第一个如果。*/

/*正向BFS
#include<iostream>
#include<queue>
#include<string.h>
#define ll long long
using namespace std;
ll n, m, cnt;
ll vis[20000005], ans[20000005];
queue<ll>a;
int main()
{
while (cin >> n >> m)
{
memset(vis, 0, sizeof(vis));
memset(ans, 0, sizeof(vis));
while(!a.empty())
a.pop();
cnt = 0;
if (n >= m)
cout << n - m << endl;
else
{
a.push(n);
while (!a.empty())
{
ll x = a.front();
a.pop();
if (x == m)
{
cnt = ans[x];
break;
} if (x <= 2*m && x - 1 >= 0 && vis[x] == 0)
{
a.push(x - 1);
ans[x - 1] = ans[x] + 1;
}
if (x <= 2*m && x != 0 && vis[x] == 0)
{
a.push(x * 2);
ans[x * 2] = ans[x] + 1;
}
vis[x] = 1;
}
cout << cnt << endl;
} }
return 0;
}
*/ //逆向规律模拟
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#define ll long long
using namespace std;
ll a, b, ans;
int main()
{
while (cin >> a >> b)
{
ans = ;
if (a >= b)
ans = a - b;
else
{
while (a < b)
{
if (b % )
{
ans++;
b++;
}
else
{
ans++;
b /= ;
}
}
ans += abs(a - b);
}
cout << ans << endl;
}
return ;
}

CF 520 B. Two Buttons(bfs)的更多相关文章

  1. cf.295.B Two Buttons (bfs)

     Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  2. CF Two Buttons (BFS)

    Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  3. CF #375 (Div. 2) D. bfs

    1.CF #375 (Div. 2)  D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...

  4. 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 ...

  5. CF 986A Fair——多源bfs

    题目:http://codeforces.com/contest/986/problem/A 如果从每个村庄开始bfs找货物,会超时. 发现k较小.那就从货物开始bfs,给村庄赋上dis[ 该货物 ] ...

  6. CF G. Orientation of Edges BFS

    来两遍 $BFS,$ 都贪心一下即可. #include <bits/stdc++.h> #define maxn 300009 using namespace std; void set ...

  7. CF#541 D. Gourmet choice /// BFS 拓扑

    题目大意: 给定n m 第一行有n个数 第二行有m个数 接下来n行每行m列 有 = < > 位于 i j 的符号表示 第一行第i个数与第二行第j个数的大小关系 1.将n+m个数 当做按顺序 ...

  8. jQuery+css+div--一些细节详解

    (一).首先.让我们认识一下最基本普通的alert()弹出框!(改变alert()提示弹出框的样式) 我们在写html或是jsp页面的时候,谁都不希望自己精心设计,且非常美观的页面颜色布局被破坏掉吧! ...

  9. cf520B-Two Buttons 【BFS】

    http://codeforces.com/contest/520/problem/B Two Buttons Vasya has found a strange device. On the fro ...

随机推荐

  1. UML在实践中的现状和一些建议

    本文是我在csdn上看到的文章,由于认识中的共鸣,摘抄至此. 原文地址:http://blog.csdn.net/vrman/article/details/280157 UML在国内不少地方获得了应 ...

  2. 修改LINUX ROOT密码

    Connecting to 10.10.70.22:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]' ...

  3. windows下启动命令行

    1.当前目录打开命令窗口:shift+鼠标右键,然后打开命令窗口 2.查看监听的端口:netstat – ano

  4. WINFORM 开发模式,窗体回到默认样式方法。

    软件分为3类   客户端  网站应用  app WINFORM 主要用来只做客户端应用程序.C/S 客户端程序很重要的特点:可以操作用户电脑上的文件,执行在客户端上,电脑的配置越高执行就越流畅. 在p ...

  5. STM32 C++编程 005 I2c(Soft)类

    使用 C++ 语言给 STM32 编写一个 I2c(Soft)类 我使用的STM32芯片:STM32F103ZET6 我们使用的STM32库版本:V3.5.0 注意: 想学习本套 STM32 C++编 ...

  6. R: 关于 ggplot2 的初探

    生活还很长,别急,慢慢来.亲爱的 require(ggplot2)p1 <- ggplot(mpg, aes(displ, hwy)) + geom_point() ; p1p1 + scale ...

  7. linux ftp、sftp、telnet服务开通、更改Orale最大连接数

    1 ftp服务开通 1.1 检测vsftpd是否安装及启动 先用service vsftpd status 来查看ftp是否开启.也可以使用ps -ef | grep ftp 来查看本地是否含有包含f ...

  8. Entity Framework Tutorial Basics(36):Eager Loading

    Eager Loading: Eager loading is the process whereby a query for one type of entity also loads relate ...

  9. 自己封装一个MySignal函数,方便以后直接copy.

    传统的signal可能会有信号未决或者信号重入或多或少的问题,毕竟这个函数已经很多年了. 所以推荐使用sigaction函数,但是sigaction函数相对signal较为复杂,而且每次要写一大堆.因 ...

  10. Microsoft.Baidu.Ali.语音识别/人脸识别

    在第一篇博客里提过图片识别的底层.最精准的图片识别需要海量的数据磨炼.自己写的底层没有以亿为单位的数据支持其实也是个残废品. 此篇不是为了教学.而且在需要的时候抄下来就能用 在此介绍Microsoft ...