/*题意:一个数,就是输入的第一个数,让它变成第二个数最少用几步。可以点红色按钮,蓝色按钮来改变数字,红色:*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. 数据库开源框架ormlite

    今天听说了ORM框架ORMLITE,特地去了解了一下. 该框架可以使用注解方式来生成数据库表,还封装了常用的数据库操作. 类似J2EE的HIBERNATE框架对数据库的处理. 省去了书写建表语句的麻烦 ...

  2. loader的意义和内部机制浅析

    意义: loader可以异步的加载数据到我们的activity或者fragment上面,让加载数据的时候ui线程不阻塞. 而且当数据发生变化的时候,还可以及时更新 具体用法参考 http://deve ...

  3. PAT-l3-002堆栈

    L3-002. 堆栈 大家都知道“堆栈”是一种“先进后出”的线性结构,基本操作有“入栈”(将新元素插入栈顶)和“出栈”(将栈顶元素的值返回并从堆栈中将其删除).现请你实现一种特殊的堆栈,它多了一种操作 ...

  4. __get(),__set(),__isset(),__unset()

    __get(),__set(),__isset(),__unset() 在给不可访问属性赋值时,__set()会被调用读取不可访问属性的值时,__get()会被调用 当对不可访问属性调用isset() ...

  5. c# 一维数组,二维数组,多维数组。

    数组就是给一个变量定义多个字符,可以是string也可以是int.或者说是一组变量. 可以更加方便的操作大量数据. 数组的定义1.数组里面的内容必须是同一类型2.数据必须有长度限制 一维数组 *一.数 ...

  6. 《Effective Java》第11章 序列化

    "将一个对象编码成一个字节流",称作将该对象序列化(serializing); 相反的处理过程被称作反序列化(deserializing),一旦对象被序列化后,它的编码就可以从一台 ...

  7. Spring学习大纲

    1.BeanFactory 和 FactoryBean? 2.Spring IOC 的理解,其初始化过程? 3.BeanFactory 和 ApplicationContext? 4.Spring B ...

  8. 抓包工具 Fiddler 使用:弱网络环境模拟限速测试流程

    转自:http://www.51testing.com/html/80/n-3726980.html   抓包工具 Fiddler 使用:弱网络环境模拟限速测试流程 发表于:2018-6-06 11: ...

  9. [转]VisualSVN错误 Cannot query proxy blanket解决办法

    最近重新做了一下系统,在安装和使用svn过程中遇到了一些问题,记下备忘. 第一次安装好系统之后,安装VisualSVN遇到报错: Custom action InstallWMISchemaExcut ...

  10. mysql服务启动不了解决方法

    sudo lsof |grep deleted  找占用大的kill一下,  一般是tomcat log和zookeeper的out比较吃磁盘 du -h --max-depth=1 / 今天作死,想 ...