Codeforces Round #295 (Div. 2) B. Two Buttons
2 seconds
256 megabytes
standard input
standard output
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n.
Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result?
The first and the only line of the input contains two distinct integers n and m (1 ≤ n, m ≤ 104), separated by a space .
Print a single number — the minimum number of times one needs to push the button required to get the number m out of number n.
题意:
给两个数,n,m,让n通过给点两种走步方法等于m
对n可以有两种操作:
1. 减1
2.乘2
我只会BFS。。。
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<queue>
using namespace std;
const int MAX=2e4+;
const int MIN=;
struct stu
{
int num;
int step;
}start,one,two;
queue<struct stu>q;
bool vis[MAX+];
void bfs(int n,int m)
{
memset(vis,false,sizeof(vis));
start.num = n;
start.step = ;
vis[n]=;
q.push(start);
while(!q.empty())
{
one=q.front();
two=q.front();
q.pop();//删队首
one.num*=;
one.step++;
two.num-=;
two.step++;
if(one.num==m)
{
printf("%d\n",one.step);
break;
}
if(two.num==m)
{
printf("%d\n",two.step);
}
if(one.num> && one.num<MAX && !vis[one.num])
{
q.push(one);
vis[one.num]=;
}
if(two.num> && two.num<MAX && !vis[two.num])
{
q.push(two);
vis[two.num]=;
}
}
} int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
if(n>m)
{
printf("%d\n",n-m);
}
else if(n==m)
printf("0\n");
else
{
bfs(n,m);
}
}
return ;
}
Codeforces Round #295 (Div. 2) B. Two Buttons的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #295 (Div. 2) B. Two Buttons 520B
B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 【记忆化搜索】Codeforces Round #295 (Div. 2) B - Two Buttons
题意:给你一个数字n,有两种操作:减1或乘2,问最多经过几次操作能变成m: 随后发篇随笔普及下memset函数的初始化问题.自己也是涨了好多姿势. 代码 #include<iostream> ...
- Codeforces Round #295 (Div. 2) B. Two Buttons (DP)
题意:有两个正整数\(n\)和\(m\),每次操作可以使\(n*=2\)或者\(n-=1\),问最少操作多少次使得\(n=m\). 题解:首先,若\(n\ge m\),直接输出\(n-m\),若\(2 ...
- Codeforces Round #295 (Div. 2)
水 A. Pangram /* 水题 */ #include <cstdio> #include <iostream> #include <algorithm> # ...
- codeforces 521a//DNA Alignment// Codeforces Round #295(Div. 1)
题意:如题定义的函数,取最大值的数量有多少? 结论只猜对了一半. 首先,如果只有一个元素结果肯定是1.否则.s串中元素数量分别记为a,t,c,g.设另一个串t中数量为a',t',c',g'.那么,固定 ...
- Codeforces Round #295 (Div. 2)C - DNA Alignment 数学题
C. DNA Alignment time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces Round #295 (Div. 2)A - Pangram 水题
A. Pangram time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
随机推荐
- Django 之 中间件
一.概念 1.什么是中间件? 官方解释:中间件是用来处理Django的请求和响应的框架级别的钩子.基于全局范围产生影响. 平民解释:中间件是帮助我们在视图函数执行前和执行后做的操作.它本质上就是一个自 ...
- spring MVC (学习笔记)
web.xml 相关配置 <?xml version="1.0" encoding="UTF-8"?><web-app xmlns=" ...
- YARN commands are invoked by the bin/yarn script.
Apache Hadoop 2.9.0 – YARN Commands http://hadoop.apache.org/docs/stable/hadoop-yarn/hadoop-yarn-sit ...
- 简说 call() 、apply() 、bind()
对于这三个方法,我想一部分人还是比较陌生的. 所以今天来个简单的介绍~ 我们可以将call()和apply()看作是某个对象的方法,通过调用方法的形式来间接调用函数.call()和apply()的第一 ...
- linux地址映射1、2、3(⭐⭐⭐)
欢迎关注瘋耔新浪微博:http://weibo.com/cpjphone 一.线性映射与非线性映射 ...
- Spring Boot2.0+Redis+Ehcache实现二级缓存
EHCache 本地缓存 Redis 分布式缓存(可以共享) 一级 Redis 二级Ehcache 当redis挂了 有备胎 反之: 先走本地,本地没有再走网络 尽量少走Redis 效率会高 ...
- dedecms5.7二级域名文章图片不显示修改方法.相对路径改为绝对路径的方法
dedecms5.7(织梦CMS5.7)二级域名文章图片不显示修改方法.相对路径改为绝对路径的方法 dedecms升级到5.7SP1后,开启二级域名,你会发现,在二级域名下的文章,上传的图片地址都是: ...
- 禁止屏幕旋转并同时解决以至于导致Activity重启的方法
1.禁止屏幕旋转在AndroidManifest.xml的每一个需要禁止转向的Activity配置中加入android:screenOrientation属性. //landscape(横向)port ...
- [USACO2012 OPEN] Bookshelf
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2678 [算法] 首先不难想到如下DP : 记f[i]表示前i本书的高度和最小值 显然 ...
- codevs1258关路灯
传送门 1258 关路灯 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 多瑞卡得到了一份有趣而高薪的工作.每天早晨他 ...