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\n",ans);
    }
    return 0;
}
View Code

 

posted @ 2015-01-24 16:06  sola94  阅读(235)  评论(0编辑  收藏  举报