zoukankan      html  css  js  c++  java
  • CodeForces 520B Two Buttons

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

    Description

    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.

    Sample Input

    Input
    4 6
    Output
    2
    Input
    10 1
    Output
    9

    Hint

    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:math 对于n < m时,从m出发,若m为偶数,m减半,否则,加1减半(对应结果加1),直到m <= n

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    int n, m;
    void solve()
    {
        cin >> n >> m;
        if(n >= m) {cout << n - m << endl;return;}
        int ans = 0;
        while(n < m)
        {
            if(m & 1) {ans++;m++;}
            m >>= 1;
            ans++;
        }
        ans += n - m;
        cout << ans << endl;
    }
    int main()
    {
        solve();
        return 0;
    }
    View Code

    思路2:bfs + 剪枝(记忆化)

    /*times    memy
      78ms     2104k
      by orc
      */
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <cstring>
    using namespace std;
    int n ,m;
    bool vis[20005];//此处的vis标记数组并不是像以往一样标记有没有做过而做到不走重复路径
    struct node{       //这里是做备忘录,记忆化搜索
        int x;
        int cnt;
    };
    void bfs(node v)
    {
        memset(vis,false,sizeof vis);
        queue<node> que;
        que.push(v);
        node now, nex;
        while(!que.empty())
        {
            now = que.front();
            que.pop();
            if(vis[now.x]) continue;
            if(now.x <= 0) continue;//既然要做备忘录,那么下标就不能为0
            if(now.x > m){          //重要剪枝,若now.x > m, 即now.x * 2就没必要入队,只能通过 - 1 来达到状态m
                    nex.x = now.x - 1;
                    nex.cnt = now.cnt + 1;
                    que.push(nex);
                    continue;
                }
            if(now.x == m) {cout << now.cnt << endl; return;}
            nex.x = now.x - 1;
            nex.cnt = now.cnt + 1;
            que.push(nex);
            nex.x = now.x * 2;
            nex.cnt = now.cnt + 1;
            que.push(nex);
            vis[now.x] = true;//循环结尾处对当前出队元素now标记
        }
    }
    int main()
    {
        cin >> n >> m;
        if(n >= m) cout << n - m << endl;
        else
        {
            node v;
            v.x = n;
            v.cnt = 0;
            bfs(v);
        }
    }
    View Code
  • 相关阅读:
    方法返回值使用哪个关键字?
    Java中带参数的方法和JavaScript中带参数的函数有什么不同?
    如何调用方法
    Java中如何声明方法?JavaScript中如何声明函数?
    为什么编程语言中要有方法
    什么叫方法.
    说说字符常量和字符串常量的区别
    什么是JDK?什么是JRE?说说它们之间的区别?
    Go 错误处理
    Go Defer
  • 原文地址:https://www.cnblogs.com/orchidzjl/p/4458793.html
Copyright © 2011-2022 走看看