Codeforces Round #295 (Div. 2) B. Two Buttons 520B
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.
4 6
2
10 1
9
In the first example you need to push the blue button once, and then push the red button once.
In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
本题的题意,有台带两个不同颜色按钮的装置,按红色按钮对当前数乘2,按蓝色按钮对当前数减1,且得到的数为正数。
给定一个数n,用该装置进行操作,进行若干次操作后使该数正好等于数m,问所需最少的操作步数。
这题我的想法是用bfs来做,网上有说用贪心做,没细看。前几次交的代码各种tlm,没进行良好的剪枝。
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
const int MAX_N = ;
int vis[MAX_N]; int main(){
// freopen("D:\\in.txt","r",stdin);
// freopen("D:\\out.txt","w",stdout);
memset(vis,,sizeof(vis));
queue<int> q;
int n,m;
cin>>n>>m;
bool ans = false;
vis[n] = ;
q.push(n);
if(n >= m)
cout<<n-m<<endl;
else{
while(q.size()){
int a = q.front();
q.pop(); if(a == m){
ans = true;
break;
}
int temp = a*;
if(temp > && temp < m * && vis[temp]==){
vis[temp] = vis[a]+;
q.push(temp);
}
temp = a-;
if(temp > && temp < m * && vis[temp]==){
vis[temp] = vis[a]+;
q.push(temp);
}
}
} if(ans)
cout<<vis[m]-<<endl;
return ;
}
Codeforces Round #295 (Div. 2) B. Two Buttons 520B的更多相关文章
- 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
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 ...
随机推荐
- LeetCode No.130,131,132
No.130 Solve 被围绕的区域 题目 给定一个二维的矩阵,包含 'X' 和 'O'(字母 O). 找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充. 示例 X X ...
- BCM93349DCM 手动升级 Fireware 指导
PC:Personal Computer(这里用的Win7) CM:Cable MODEM(芯片:BCM93349DCM) 一.预置条件 1.PC上已安装TFTP Server,比如tftpd32: ...
- Spatial crowdsourcing
空间众包(Spatial crowdsourcing)分类 空间众包是将一组空间任务众包给一组工作人员的过程,这要求工作人员实际位于该位置以执行相应的任务. 空间众包可以根据员工的动机分为两类:基于奖 ...
- [LC] 541. Reverse String II
Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...
- linux下文件夹归置方式
/bin:二进制可执行命令./dev:设备特殊文件./etc:系统管理和配置文件./etc/rc.d:启动的配 置文件和脚本./home:用户主目录的基点,比如用户user的主目录就是/home/us ...
- Derby数据库的使用
一. Derby数据库平台的搭建 ● JDK 1.6版本及之后的版本为Java平台提供了一个数据库管理系统,简称Derby数据库. ● 连接Derby数据库需要有关的类,这些类以jar文件的形 ...
- mvn测试常用命令
-Dmaven.test.failure.ignore=true 测试报错忽略 例子: mvn package -DAPP_ENV=dev -Dmaven.test.failure.ignore=t ...
- [洛谷P4782] [模板] 2-SAT 问题
NOIp后第一篇题解. NOIp我考的很凉啊...... 题目传送门 之前讲过怎么判断2-SAT是否存在解. 至于如何构造一组解: 我们想到对tarjan缩点后的图进行拓扑排序. 那么对于代表0状态的 ...
- jquery mobile AJAX特性的陷阱
简单情况是 MVC 重定向,URL不变 试了N种方式,跳来跳去,无解,服务端跳,写JS跳,生成跳转中间页跳.失败 后来一看,明明已经跳到新页了,样式什么还是原页的,有点火大了. 出去溜一圈,喝杯水,和 ...
- less 的使用方法总结
一. 安装和使用 LESS 1.1 安装 使用命令行安装 LESS npm install -g less 1.2 使用 less 有多种的使用方法,在这里我向大家介绍最常用的俩种方法. 第一种是直接 ...