/*题意:一个数,就是输入的第一个数,让它变成第二个数最少用几步。可以点红色按钮,蓝色按钮来改变数字,红色:*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. oracle DCL-(grant、revoke )

    1.授权GRANT <权限列表> to <user_name>; 2.收回权限REVOKE <权限列表> from <user_name>

  2. 在页面所有元素加载完成之后执行某个js函数

    在页面所有元素加载完成之后执行某个js函数 http://lgscofield.iteye.com/blog/1884352

  3. day18-事务与连接池 5.关于不考虑事务隔离性出现的问题

  4. WOJ 18 动态无向图

    一开始我是不会写的,后来点开了题解: 无话可说……那就写吧……然而第一发跑成暴力分,后来加了一个优化:就是在询问里面提到过的边都不用再加了. 然后……然后就过了呀…… 其实还有面向数据的编程的骚操作… ...

  5. 使用database control配置数据库时 要求在当前oracle主目录中配置监听程序

    1:配置本地的环境变量 打开cmd命令界面  C:\Users\gechong>lsnrctl start 这时候报适配器错误 2.在cmd中输入 tnslsnr命令

  6. 安装visual_Paradigm,时序图的应用

    此安装包已经上传到sunny的百度网盘. 删除,即,右击别的地方,然后,选择delete即可. 拖箭头,拖到某个实体上,再松开,会自动连线. 很好的一款画图工具.

  7. Ubuntu12.04不能连接小米开发(转)

    发现Ubuntu12.04不能连接小米开发,adb devices不能看到设备!  搞了一个上午才搞成功! 小米手机利用USB连接到Ubuntu 12.04系统.运行下面的命令: longskywan ...

  8. Bootstrap 组件之 List group

    简介 List group 指列表.当然,Bootstrap 列表不局限于由 <ul> 和 <li> 标签构成的. Bootstrap 中一共三种列表的构成方式,这里 有一个例 ...

  9. Ubuntu16.04版安装VMwareTools的步骤和没法挂载目录问题的解决

    vmtool安装流程 1.点击vmware 里面的虚拟机——>安装vmware tool 2.然后(等待一会)弹出一个界面把里面的 VMwareTools-9.6.1-1378637.tar.g ...

  10. 找BUG

    找一找BUG 一段代码,实现一个pop,push,和getmin都是O(1)的方法. 最初源代码 伙伴代码如下,代码的地址可以通过这个访问: Ubuntu Pastebin https://paste ...