CF 520 B. Two Buttons(bfs)
/*题意:一个数,就是输入的第一个数,让它变成第二个数最少用几步。可以点红色按钮,蓝色按钮来改变数字,红色:*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)的更多相关文章
- cf.295.B Two Buttons (bfs)
Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- CF Two Buttons (BFS)
Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- CF #375 (Div. 2) D. bfs
1.CF #375 (Div. 2) D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...
- 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 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#541 D. Gourmet choice /// BFS 拓扑
题目大意: 给定n m 第一行有n个数 第二行有m个数 接下来n行每行m列 有 = < > 位于 i j 的符号表示 第一行第i个数与第二行第j个数的大小关系 1.将n+m个数 当做按顺序 ...
- jQuery+css+div--一些细节详解
(一).首先.让我们认识一下最基本普通的alert()弹出框!(改变alert()提示弹出框的样式) 我们在写html或是jsp页面的时候,谁都不希望自己精心设计,且非常美观的页面颜色布局被破坏掉吧! ...
- cf520B-Two Buttons 【BFS】
http://codeforces.com/contest/520/problem/B Two Buttons Vasya has found a strange device. On the fro ...
随机推荐
- 每天一道算法题(15)——打印1到最大的n位数
题目: 打印1到最大的n位数.如n=4,打印1-9999. 思路: 由于直接使用循环会导致int或者long long都不够存储.因此使用字符串来存储数据,这里涉及到数字转换成字符串以及字符串的加法. ...
- 卸载phonegap
npm uninstall cordova -gnpm uninstall phonegap -g
- GitHub Blog创建以及本地管理(转)
GitHub Blog创建以及本地管理 创建 注册GitHub账户 首页点击新建仓库 New repository repository name必须为 Owner.github.io EX:我的 ...
- Tensorflow手写数字识别训练(梯度下降法)
# coding: utf-8 import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data #p ...
- Struts第三天
OgnlValueStack贯穿整个 Action 的生命周期. 它是ContextMap中的一部分,里面的结构是一个List,是我们可以快速访问数据一个容器.它的封装是由struts2框架完成的. ...
- ROS Learning-029 (提高篇-007 A Mobile Base-05) 控制移动平台 --- (Python编程)控制虚拟机器人的移动(精确的制定目标位置)
ROS 提高篇 之 A Mobile Base-05 - 控制移动平台 - (Python编程)控制虚拟机器人的移动(精确的制定目标位置) 使用 odometry 消息类型 重写 out_and_ba ...
- Windows 安装 Microsoft Visual Studio 2010
Windows 安装 VS2010 我的电脑系统:Windows 10 x64位 我要安装的Microsoft Visual Studio 是:VS2010 注意: VS2010 没有专门的64位版. ...
- Apache Thrift with Java Quickstart(thrift入门及Java实例)
thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl ...
- ZROI2018提高day4t2
传送门 分析 我们二分球的直径,然后就像奶酪那道题一样,将所有距离相遇直径的点用并查集连在一起,然后枚举所有与上边的顶距离小于直径的点和所有与下边的距离小于直径的点,如果它们被并查集连在一起则代表这个 ...
- c# 的默认访问修饰符小结(转)
c# 的访问修饰符是private 还是 internal? 准确的说,不能一概而论. [MSDN] Classes and structs that are not nested within ot ...