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 ...
随机推荐
- Spring中的事务管理(学习笔记)
什么是事物? 事物是指逻辑上的一组操作,这组操作要么全部成功,要么全部失败. 事物的特性: 原子性.一致性.隔离性.持久性 Spring事务管理的高级接口: PlatformTransactionMa ...
- Class.forName("java.lang.String")的作用?
返回字节码: 返回的方式有2种: 第一种是这个类的字节码已经加载到内存里面来了,现在想要取到它的字节码,我直接找到那份字节码把他返回: 第二种是我去得到这个类的字节码,结果在虚拟机里面还没有这个类的字 ...
- (linux)自旋锁及其衍生锁
自旋锁 毫秒以下. 自旋锁用于多个CPU系统中,在单处理器系统中,自旋锁不起锁的作用,只是禁止或启用内核抢占.在自旋锁忙等待期间,内核抢占机制还是有效的,等待自旋锁释放的线程可能被更高优先级的线程 ...
- proc_create的使用方法
proc_create的使用方法 proc文件系统是个有用的东东.创建一个proc虚拟文件,应用层通过读写该文件,即可实现与内核的交互.proc虚拟文件是如何创建的呢? 先看看比较简单的,创建proc ...
- 异常、Throwable、finally、File类(十九)
1.异常的概述和分类 * A:异常的概述 * 异常就是Java程序在运行过程中出现的错误.* B:异常的分类 * 通过API查看Throwable * Error * 服务器宕机,数据库崩溃等 * E ...
- ODC(Orthogonal Defect Classification)简介——正交缺陷分类法
Defect分析是软件开发和测试中一个重要的环节,ODC介绍了一种不同于大家常用的非常有效的defect分类及分析方法.这篇文章简单的向大家介绍了什么是ODC,以及如何在项目和产品开发中使用ODC来改 ...
- Python之如果添加扩展包
1.首先下载好你需要的扩展包 下载地址是http://www.lfd.uci.edu/~gohlke/pythonlibs/ 2.将你下载好的.whl文件放在你的python文件夹中的Lib\site ...
- Watir: Watir webdriver对JS 弹出框的操作现在非常简单。
以下代码支持Firefox,IE,Chrome require 'watir-webdriver' #require "watir-webdriver/extensions/alerts&q ...
- 让ie支持css3的一些htc文件
1. Dean Edwards的IE7.js (以及 IE8.js, IE9.js)这个玩意估计是试图让IE支持CSS3属性的鼻祖,还算蛮强大,就是性能开销较大,要解析很多文件脚本,给DOM添加大量的 ...
- View Programming Guide for iOS ---- iOS 视图编程指南(三)---Windows
Windows Every iOS application needs at least one window—an instance of the UIWindow class—and some m ...