码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces 507C Guess Your Way Out!(二叉树)

时间:2015-01-24 17:07:53      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

 

实在太困 题都看错

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

技术分享
#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

 

CodeForces 507C Guess Your Way Out!(二叉树)

标签:

原文地址:http://www.cnblogs.com/sola1994/p/4246028.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!