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 ...
随机推荐
- thread常用方法
- 基于C++求两个数的最大公约数最小公倍数
求x,y最大公约数的函数如下: int gys(int x,int y) { int temp; while(x) {temp=x; x=y%x; y=temp;} return y; } x=y的时 ...
- 无法解决 equal to 操作中 "Chinese_PRC_CI_AS" 和 "Chinese_PRC_BIN" 之间的排序规则冲
在两个数据库之间进行复合查询时有时会出现如下错误: 无法解决 equal to 操作中 "Chinese_PRC_CI_AS" 和 "Chinese_PRC_BIN&qu ...
- bootstrap媒体查询常用写法
@media (max-width: 768px) { /*超小屏幕设备 手机*/ } @media (min-width: 768px) and (max-width: 992px) { /*小屏幕 ...
- c语言输入数据
比如要求输入一行数据(注意:没有给出输入多少个),并且以空格隔开那么就可以如下进行判断 '; ],temp,i=; while(c!='\n') { scanf("%d%c",&a ...
- js的学习
对于 ff的 relatedTarget 及IE的toElement fromElement DOM通过event对象的relatedTarget属性提供了相关元素的信息.这个属性只对于mou ...
- 重构get请求代码---PartyLocation
将原理在PersonDto中定义的partyLocations,剪切到PartyDto中去. @JsonApiToMany private List<PartyLocationDto> p ...
- netty中的PoolChunk
数据结构学的烂,看这个类比较的吃力 PoolChunk主要使用long allocate(int normCapacity) 在buffer pool中分配buffer.这个类有几个重要的概念:pag ...
- [译]Javascript中的switch语句
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
- git CVE-2014-9390 验证以及源码对比
一 验证部分 首先在ubuntu下面建立如下工程 mkdir repo cd repo git init mkdir -p .GiT/hooks cp post-checkout .GiT/hooks ...