zoukankan      html  css  js  c++  java
  • Codeforces Round #287 (Div. 2) C. Guess Your Way Out! 水题

    C. Guess Your Way Out!
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Amr bought a new video game "Guess Your Way Out!". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height h. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node.

    Let's index all the leaf nodes from the left to the right from 1 to 2h. The exit is located at some node n where 1 ≤ n ≤ 2h, the player doesn't know where the exit is so he has to guess his way out!

    Amr follows simple algorithm to choose the path. Let's consider infinite command string "LRLRLRLRL..." (consisting of alternating characters 'L' and 'R'). Amr sequentially executes the characters of the string using following rules:

    • Character 'L' means "go to the left child of the current node";
    • Character 'R' means "go to the right child of the current node";
    • If the destination node is already visited, Amr skips current command, otherwise he moves to the destination node;
    • If Amr skipped two consecutive commands, he goes back to the parent of the current node before executing next command;
    • If he reached a leaf node that is not the exit, he returns to the parent of the current node;
    • If he reaches an exit, the game is finished.

    Now Amr wonders, if he follows this algorithm, how many nodes he is going to visit before reaching the exit?

    Input

    Input consists of two integers h, n (1 ≤ h ≤ 50, 1 ≤ n ≤ 2h).

    Output

    Output a single integer representing the number of nodes (excluding the exit node) Amr is going to visit before reaching the exit by following this algorithm.

    Sample test(s)
    Input
    1 2
    Output
    2
    Input
    2 3
    Output
    5
    Input
    3 6
    Output
    10
    Input
    10 1024
    Output
    2046
    Note

    A perfect binary tree of height h is a binary tree consisting of h + 1 levels. Level 0 consists of a single node called root, level h consists of 2h nodes called leaves. Each node that is not a leaf has exactly two children, left and right one.

    Following picture illustrates the sample test number 3. Nodes are labeled according to the order of visit.

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 100001
    const int inf=0x7fffffff;   //无限大
    int main()
    {
        ll h,n;
        cin>>h>>n;
        ll t=1;
        for (int i=0;i<h;i++)
            t*=2;
        ll ans=0;
        int hehe=1;
        for(int i=0;i<h;i++) {
            t/=2;
            if(n>t)
            {
                if (hehe==1)
                    ans+=t*2;
                else
                    ans++;
                n-=t,hehe=1;
            } else
            {
                if(hehe==0)
                    ans+=t*2;
                else
                    ans++;
                hehe=0;
            }
        }
        cout<<ans<<endl;;
        return 0;
    }
  • 相关阅读:
    顶尖操盘手买入规则
    一个网友在评论见义勇为时候应该注意事项
    20111215 白糖空头控盘下的多头陷阱(与魂灵共舞)
    近期au黄金市场的探讨(2011年12月27日)
    ASP.NET MVC 中,手动移除已注册到容器的规则方法
    VS2010功能——任务列表
    关于SQL排序,父条件对应子条件排序
    确保每一步的业务代码都能够正确执行。
    C#程序代码中常用的快捷键
    cmd 下创建新文件(不是文件夹)
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4245742.html
Copyright © 2011-2022 走看看