zoukankan      html  css  js  c++  java
  • CodeForces 507C Guess Your Way Out!(二叉树)

    实在太困 题都看错

    先贴王大神的代码有空再做遍

    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <cstring>
    #include <iostream>
    #include <cstring>
    #include <queue>
    using namespace std;
    typedef __int64 LL;
    LL h,n;
    LL now1,now2;
    LL ans;
    void dfs(LL q,LL w,int vis,int high,LL sum)
    {
        if(high == h)
        {
            return;
        }
        LL mid = (q + w)/2;
        if(vis == 0)
        {
            if(n <= mid)
            {
                ans += (sum * 2 - 2)/2 + 1;
                dfs(q,mid,0,high + 1,sum/2);
            }
            else
            {
                ans++;
                dfs(mid+1,w,1,high + 1,sum/2);
            }
        }
        else
        {
            if(n <= mid)
            {
                ans++;
                dfs(q,mid,0,high + 1,sum/2);
            }
            else
            {
                ans += (sum * 2 - 2)/2 + 1;
                dfs(mid+1,w,1,high + 1,sum/2);
            }
        }
    }
    int main()
    {
        int i;
        while(scanf("%I64d %I64d",&h,&n)!=EOF)
        {
            ans = 0;
            now1 = 1;
            now2 = 1;
            for(i = 0; i < h; i++)
            {
                now2 *= 2;
            }
           
            LL m = (now1 + now2)/2;
            if(n <= m)
            {
                ans += 1;
                dfs(now1,m,0,1,now2/2);
            }
            else
            {
                ans += (((now2 * 2) - 2)/2) + 1;
          
                dfs(m+1,now2,1,1,now2/2);
            }
            printf("%I64d
    ",ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    5-4 链表的天然递归结构性质
    java8 Optional 解析
    [leetcode] Unique Binary Search Trees
    [leetcode] Anagrams
    [leetcode] Valid Number
    构造素数表2
    构造素数表1
    整型所占字节
    习题7-8 字符串转换成十进制整数
    习题7-7 字符串替换
  • 原文地址:https://www.cnblogs.com/sola1994/p/4246028.html
Copyright © 2011-2022 走看看