CodeForces - 520B 按钮 [广度优先搜索/贪心]

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?

Input

The first and the only line of the input contains two distinct integers n and m (1 ≤ n, m ≤ 104), separated by a space .

Output

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.

Example

Input

4 6

Output

2

Input

10 1

Output

9

Note

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.

做法一:

贪心

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5 long long a,n,m;
 6 scanf("%lld%lld",&n,&m);
 7 a=0;
 8 while(n<m)
 9 {
10 if(m%2==1)
11  m++;
12 else
13  m=m/2;
14 a++;
15 }
16 printf("%lld\n",a+n-m);
17 }

做法二:

BFS

BFS训练题里有一道类似的题

需要注意的两点:

一是把控好下界,不要出现比0小的情况

二是把控好一个合适的上界,不要叫程序无限制的一直往大的离谱的数字上去搜索

做好以上两点剪枝,就没有问题了

代码:

#include<bits/stdc++.h>
using namespace std;
#define read(x) scanf("%lld",&x)
#define out(x) printf("%lld",&x)
#define cfread(x) scanf("%I64d",&x)
#define cfout(x) printf("%I64d",&x)
#define mian main
#define min(x,y) (x<y?x:y)
#define max(x,y) (x<y?y:x)
#define f(i,p,q,t) for(i=p;i<q;i+=t)
#define MAXN 110000
#define inf 0x3f3f3f3f
#define mem(x,t) memset(x,t,sizeof(x));
#define T true
#define F false
#define def -1*inf
map<int,int> v;
queue< pair<int,int> > team;//队列
int main(){
    int l,r;
    cin>>l>>r;
    team.push(make_pair(l,0));
    v[l] = 1;
    int ans = -1;
    while(!team.empty()){
        pair<int,int> x = team.front();
        team.pop();
        if(x.first==r){
            ans = x.second;
            break;
        }
        pair<int,int> nx = make_pair(2*x.first,x.second+1);
        if(!(v[nx.first]||nx.first>3*r)){
            team.push(nx);
            v[nx.first]=1;
        }
        nx = make_pair(x.first-1,x.second+1);
        if(!(v[nx.first]||nx.first<0)){
            team.push(nx);
            v[nx.first]=1;
        }
    }
    cout<<ans;
//  return 0;
    return 0;
}

原文地址:https://www.cnblogs.com/xfww/p/8531247.html

时间: 2024-07-31 04:46:00

CodeForces - 520B 按钮 [广度优先搜索/贪心]的相关文章

无向图的深度优先与广度优先搜索代码实现

图采用了邻接表的形式储存. 带不带权都无所谓的 深度优先搜索 Depth First Search 道理和树的先序遍历差不多,把将要访问的点入栈,然后从栈里取点进行访问. 由于这只是类中的一个成员函数,有些被调用的函数的具体代码将会在文章最后补上 ,但是函数功能看注释就好了 1 //深度优先 2 void GraphAdjacencyListWeight::DFSAdvanced(int StartVertex) { 3 int *visited = new int[VertexNumber];

SDUT 2141 【TEST】数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历

数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Description 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜索(BFS)遍历,输出从某个顶点出发的遍历序列.(同一个结点的同层邻接点,节点编号小的优先遍历) Input 输入第一行为整数n(0< n <100),表示数据的组数.对于每组数据,第一行是三个整数k,m,t(0<

数据结构实验之图论二:基于邻接表的广度优先搜索遍历

数据结构实验之图论二:基于邻接表的广度优先搜索遍历 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜索(BFS)遍历,输出从某个顶点出发的遍历序列.(同一个结点的同层邻接点,节点编号小的优先遍历) 输入 输入第一行为整数n(0< n <100),表示数据的组数. 对于每组数据,第一行是三个整数k,m,t(0<k<100,0<m<(k-1)*k/2,

图算法系列-深度优先搜索与广度优先搜索

2.深度优先搜索 为了访问一个顶点,我们将它标记为已经访问过,然后递归的访问所有与子邻接的并且尚未标记的顶点,这就是深度优先搜索(DFS),DFS常用于解决路径问题. 比如下面的连通图,我们从顶点0开始对图进行探索 下面这个图显示了DFS处理时的递归调用树. DFS可以解决的问题:1)环检测:一个图中有环吗?该图是森林吗?2)简单路径:给定两个顶点,是否存在一条连接他们的路径3)简单连通性:无论何时使用DFS,都可以在线性时间内确定一个图是否连通4)顶点搜索:在给定顶点所在的同一个连通分量中有多

图的遍历(深度优先与广度优先搜索两种方案)

1.图的遍历--深度优先搜索 import java.util.Scanner ; public class Map{ static int n ; static int m ; static int[] book ; static int[][] e ; public static void mapDfs(int cur){ //深度优先搜索思想核心: System.out.print(cur + " ") ; for (int i=1;i<=n;i++) { if (e[cu

矩阵图中的广度优先搜索

经常会有类似的题目,如迷宫问题,在一个矩阵图中给定出发点和目标点,每次只能上下左右移动,求到目标点的最短走法,或者说是一共有多少种走法. 思路其实很简单,深搜.广搜.相对比较而言,广度优先搜索更加实用于求最短的走法(步数) 在矩阵图中的广搜需要注意一下几点. 1.确定每步的走法:不同题的走法可能不同,每次搜索时将走法保存在数组中. 2.确定初始状态 往往注意刚开始得起点(i,j)必须把MAP(i,j)改变为 -1(或者其他的),栈中第一个元素即为初始状态 3.保存状态.这点很重要.需要用数组或者

Codeforces 442B Andrey and Problem(贪心)

题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,现在他有n个朋友,每个朋友想出题目的概率为pi,但是他可以同时向多个人寻求帮助,不过他只能要一道题,也就是如果他向两个人寻求帮助,如果两个人都成功出题,也是不可以的. 解题思路:贪心,从概率最大的人开始考虑,如果询问他使得概率变大,则要询问. #include <cstdio> #include <cstring> #include <a

地牢逃脱(BFS(广度优先搜索))

题目描述 给定一个 n 行 m 列的地牢,其中 '.' 表示可以通行的位置,'X' 表示不可通行的障碍,牛牛从 (x0 , y0 ) 位置出发,遍历这个地牢,和一般的游戏所不同的是,他每一步只能按照一些指定的步长遍历地牢,要求每一步都不可以超过地牢的边界,也不能到达障碍上.地牢的出口可能在任意某个可以通行的位置上.牛牛想知道最坏情况下,他需要多少步才可以离开这个地牢. 输入描述: 每个输入包含 1 个测试用例.每个测试用例的第一行包含两个整数 n 和 m(1 <= n, m <= 50),表示

图的广度优先搜索(BFS)

把以前写过的图的广度优先搜索分享给大家(C语言版) 1 #include<stdio.h> 2 #include<stdlib.h> 3 #define MAX_VERTEX_NUM 20 4 #define MAXQSIZE 100 5 #define OK 1 6 typedef char VertexType; 7 typedef int QElemType; 8 9 typedef struct ArcNode//边结点 10 { 11 int adjvex; 12 str